-
Notifications
You must be signed in to change notification settings - Fork 238
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Convert Cloud resources to new "resource" framework
- Puts the cloud client validation into the "cloud" package (private) - Makes sure all resources have an ID helper (to generate imports) - Paves the way for Terraform code gen
- Loading branch information
1 parent
e9aeecc
commit f414600
Showing
23 changed files
with
460 additions
and
320 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
terraform import grafana_cloud_stack.stack_name {{stack_id}} // import by numerical ID | ||
terraform import grafana_cloud_stack.stack_name {{stack_slug}} // or import by slug | ||
terraform import grafana_cloud_stack.name "{{ stackSlugOrID }}" |
1 change: 1 addition & 0 deletions
1
examples/resources/grafana_cloud_stack_service_account/import.sh
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
terraform import grafana_cloud_stack_service_account.name "{{ stackSlug }}:{{ serviceAccountID }}" |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
package common | ||
|
||
import ( | ||
"fmt" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
) | ||
|
||
var allResources = []*Resource{} | ||
|
||
type Resource struct { | ||
Name string | ||
IDType *TFID | ||
Schema *schema.Resource | ||
} | ||
|
||
func NewResource(name string, idType *TFID, schema *schema.Resource) *Resource { | ||
r := &Resource{ | ||
Name: name, | ||
IDType: idType, | ||
Schema: schema, | ||
} | ||
allResources = append(allResources, r) | ||
return r | ||
} | ||
|
||
func (r *Resource) ImportExample() string { | ||
id := r.IDType | ||
fields := make([]string, len(id.expectedFields)) | ||
for i := range fields { | ||
fields[i] = fmt.Sprintf("{{ %s }}", id.expectedFields[i]) | ||
} | ||
return fmt.Sprintf(`terraform import %s.name %q | ||
`, r.Name, strings.Join(fields, defaultSeparator)) | ||
} | ||
|
||
// GenerateImportFiles generates import files for all resources that use a helper defined in this package | ||
func GenerateImportFiles(path string) error { | ||
for _, r := range allResources { | ||
resourcePath := filepath.Join(path, "resources", r.Name, "import.sh") | ||
if err := os.RemoveAll(resourcePath); err != nil { // Remove the file if it exists | ||
return err | ||
} | ||
|
||
if r.IDType == nil { | ||
log.Printf("Skipping import file generation for %s because it does not have an ID type\n", r.Name) | ||
continue | ||
} | ||
|
||
log.Printf("Generating import file for %s (writing to %s)\n", r.Name, resourcePath) | ||
if err := os.WriteFile(resourcePath, []byte(r.ImportExample()), 0600); err != nil { | ||
return err | ||
} | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
package common | ||
|
||
import ( | ||
"fmt" | ||
"strconv" | ||
"strings" | ||
) | ||
|
||
var ( | ||
defaultSeparator = ":" | ||
) | ||
|
||
type TFID struct { | ||
separators []string | ||
expectedFields []string | ||
} | ||
|
||
func NewTFID(expectedFields ...string) *TFID { | ||
return newTFIDWithSeparators([]string{defaultSeparator}, expectedFields...) | ||
} | ||
|
||
// Deprecated: Use NewTFID instead | ||
// We should standardize on a single separator, so that function should only be used for old resources | ||
// On major versions, switch to NewTFID and remove uses of this function | ||
func NewTFIDWithLegacySeparator(legacySeparator string, expectedFields ...string) *TFID { | ||
return newTFIDWithSeparators([]string{defaultSeparator, legacySeparator}, expectedFields...) | ||
} | ||
|
||
func newTFIDWithSeparators(separators []string, expectedFields ...string) *TFID { | ||
tfID := &TFID{ | ||
separators: separators, | ||
expectedFields: expectedFields, | ||
} | ||
return tfID | ||
} | ||
|
||
func (id *TFID) Make(parts ...any) string { | ||
if len(parts) != len(id.expectedFields) { | ||
panic(fmt.Sprintf("expected %d fields, got %d", len(id.expectedFields), len(parts))) // This is a coding error, so panic is appropriate | ||
} | ||
stringParts := make([]string, len(parts)) | ||
for i, part := range parts { | ||
stringParts[i] = fmt.Sprintf("%v", part) | ||
} | ||
return strings.Join(stringParts, defaultSeparator) | ||
} | ||
|
||
func (id *TFID) AsInt64(resourceID string) (int64, error) { | ||
parts, err := id.Split(resourceID) | ||
if err != nil { | ||
return 0, err | ||
} | ||
return strconv.ParseInt(parts[0], 10, 64) | ||
} | ||
|
||
func (id *TFID) AsString(resourceID string) (string, error) { | ||
parts, err := id.Split(resourceID) | ||
if err != nil { | ||
return "", err | ||
} | ||
|
||
return parts[0], nil | ||
} | ||
|
||
func (id *TFID) Split(resourceID string) ([]string, error) { | ||
for _, sep := range id.separators { | ||
parts := strings.Split(resourceID, sep) | ||
if len(parts) == len(id.expectedFields) { | ||
return parts, nil | ||
} | ||
} | ||
return nil, fmt.Errorf("id %q does not match expected format. Should be in the format: %s", resourceID, strings.Join(id.expectedFields, defaultSeparator)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.