-
Notifications
You must be signed in to change notification settings - Fork 235
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
- Add typing to the resource ID system. Makes it more robust and easier to use. - Makes sure all resources have an ID helper (to generate imports) - Paves the way for Terraform code gen
- Loading branch information
1 parent
272030b
commit 5607ebf
Showing
14 changed files
with
193 additions
and
108 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
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 *ResourceID | ||
Schema *schema.Resource | ||
} | ||
|
||
func NewResource(name string, idType *ResourceID, 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].Name) | ||
} | ||
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
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
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.