New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add support for registry modules without vcs #546
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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 |
---|---|---|
|
@@ -9,6 +9,7 @@ import ( | |
tfe "github.com/hashicorp/go-tfe" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
) | ||
|
||
func resourceTFERegistryModule() *schema.Resource { | ||
|
@@ -23,19 +24,24 @@ func resourceTFERegistryModule() *schema.Resource { | |
Schema: map[string]*schema.Schema{ | ||
"organization": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
"module_provider": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
ExactlyOneOf: []string{"vcs_repo"}, | ||
RequiredWith: []string{"organization", "name"}, | ||
}, | ||
"name": { | ||
Type: schema.TypeString, | ||
Computed: true, | ||
Optional: true, | ||
ForceNew: true, | ||
}, | ||
"vcs_repo": { | ||
Type: schema.TypeList, | ||
Required: true, | ||
Optional: true, | ||
ForceNew: true, | ||
MinItems: 1, | ||
MaxItems: 1, | ||
|
@@ -59,39 +65,98 @@ func resourceTFERegistryModule() *schema.Resource { | |
}, | ||
}, | ||
}, | ||
"namespace": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
RequiredWith: []string{"registry_name"}, | ||
}, | ||
"registry_name": { | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ForceNew: true, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Pointing out |
||
RequiredWith: []string{"module_provider"}, | ||
ValidateFunc: validation.StringInSlice( | ||
[]string{"private", "public"}, | ||
true, | ||
), | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func resourceTFERegistryModuleCreate(d *schema.ResourceData, meta interface{}) error { | ||
func resourceTFERegistryModuleCreateWithVCS(v interface{}, meta interface{}) (*tfe.RegistryModule, error) { | ||
tfeClient := meta.(*tfe.Client) | ||
|
||
// Create a new options struct. | ||
// Create module with VCS repo configuration block. | ||
options := tfe.RegistryModuleCreateWithVCSConnectionOptions{} | ||
vcsRepo := v.([]interface{})[0].(map[string]interface{}) | ||
|
||
// Get and assert the VCS repo configuration block. | ||
if v, ok := d.GetOk("vcs_repo"); ok { | ||
vcsRepo := v.([]interface{})[0].(map[string]interface{}) | ||
|
||
options.VCSRepo = &tfe.RegistryModuleVCSRepoOptions{ | ||
Identifier: tfe.String(vcsRepo["identifier"].(string)), | ||
OAuthTokenID: tfe.String(vcsRepo["oauth_token_id"].(string)), | ||
DisplayIdentifier: tfe.String(vcsRepo["display_identifier"].(string)), | ||
} | ||
options.VCSRepo = &tfe.RegistryModuleVCSRepoOptions{ | ||
Identifier: tfe.String(vcsRepo["identifier"].(string)), | ||
OAuthTokenID: tfe.String(vcsRepo["oauth_token_id"].(string)), | ||
DisplayIdentifier: tfe.String(vcsRepo["display_identifier"].(string)), | ||
} | ||
|
||
log.Printf("[DEBUG] Create registry module from repository %s", *options.VCSRepo.Identifier) | ||
registryModule, err := tfeClient.RegistryModules.CreateWithVCSConnection(ctx, options) | ||
if err != nil { | ||
return fmt.Errorf( | ||
return nil, fmt.Errorf( | ||
"Error creating registry module from repository %s: %w", *options.VCSRepo.Identifier, err) | ||
} | ||
return registryModule, nil | ||
} | ||
|
||
func resourceTFERegistryModuleCreateWithoutVCS(meta interface{}, d *schema.ResourceData) (*tfe.RegistryModule, error) { | ||
tfeClient := meta.(*tfe.Client) | ||
|
||
options := tfe.RegistryModuleCreateOptions{ | ||
Name: tfe.String(d.Get("name").(string)), | ||
Provider: tfe.String(d.Get("module_provider").(string)), | ||
} | ||
|
||
if registryName, ok := d.GetOk("registry_name"); ok { | ||
options.RegistryName = tfe.RegistryName(registryName.(string)) | ||
|
||
if registryName.(string) == "public" { | ||
options.Namespace = d.Get("namespace").(string) | ||
} | ||
} | ||
|
||
orgName := d.Get("organization").(string) | ||
|
||
log.Printf("[DEBUG] Create registry module named %s", *options.Name) | ||
registryModule, err := tfeClient.RegistryModules.Create(ctx, orgName, options) | ||
|
||
if err != nil { | ||
return nil, fmt.Errorf("Error creating registry module %s: %w", *options.Name, err) | ||
} | ||
|
||
return registryModule, nil | ||
} | ||
|
||
func resourceTFERegistryModuleCreate(d *schema.ResourceData, meta interface{}) error { | ||
tfeClient := meta.(*tfe.Client) | ||
|
||
var registryModule *tfe.RegistryModule | ||
var err error | ||
|
||
if v, ok := d.GetOk("vcs_repo"); ok { | ||
registryModule, err = resourceTFERegistryModuleCreateWithVCS(v, meta) | ||
} else { | ||
registryModule, err = resourceTFERegistryModuleCreateWithoutVCS(meta, d) | ||
} | ||
Comment on lines
+143
to
+147
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like this. |
||
|
||
if err != nil { | ||
return err | ||
} | ||
|
||
err = resource.Retry(time.Duration(5)*time.Minute, func() *resource.RetryError { | ||
rmID := tfe.RegistryModuleID{ | ||
Organization: registryModule.Organization.Name, | ||
Name: registryModule.Name, | ||
Provider: registryModule.Provider, | ||
Namespace: registryModule.Namespace, | ||
RegistryName: registryModule.RegistryName, | ||
} | ||
_, err := tfeClient.RegistryModules.Read(ctx, rmID) | ||
if err != nil { | ||
|
@@ -113,6 +178,8 @@ func resourceTFERegistryModuleCreate(d *schema.ResourceData, meta interface{}) e | |
d.Set("name", registryModule.Name) | ||
d.Set("module_provider", registryModule.Provider) | ||
d.Set("organization", registryModule.Organization.Name) | ||
d.Set("namespace", registryModule.Namespace) | ||
d.Set("registry_name", registryModule.RegistryName) | ||
|
||
return resourceTFERegistryModuleRead(d, meta) | ||
} | ||
|
@@ -127,6 +194,8 @@ func resourceTFERegistryModuleRead(d *schema.ResourceData, meta interface{}) err | |
Organization: d.Get("organization").(string), | ||
Name: d.Get("name").(string), | ||
Provider: d.Get("module_provider").(string), | ||
Namespace: d.Get("namespace").(string), | ||
RegistryName: tfe.RegistryName(d.Get("registry_name").(string)), | ||
} | ||
|
||
registryModule, err := tfeClient.RegistryModules.Read(ctx, rmID) | ||
|
@@ -144,6 +213,8 @@ func resourceTFERegistryModuleRead(d *schema.ResourceData, meta interface{}) err | |
d.Set("name", registryModule.Name) | ||
d.Set("module_provider", registryModule.Provider) | ||
d.Set("organization", registryModule.Organization.Name) | ||
d.Set("namespace", registryModule.Namespace) | ||
d.Set("registry_name", registryModule.RegistryName) | ||
|
||
// Set VCS repo options. | ||
var vcsRepo []interface{} | ||
|
@@ -179,19 +250,31 @@ func resourceTFERegistryModuleDelete(d *schema.ResourceData, meta interface{}) e | |
} | ||
|
||
func resourceTFERegistryModuleImporter(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { | ||
registryModuleInfo := strings.SplitN(d.Id(), "/", 4) | ||
if len(registryModuleInfo) != 4 { | ||
return nil, fmt.Errorf( | ||
"invalid registry module import format: %s (expected <ORGANIZATION>/<REGISTRY MODULE NAME>/<REGISTRY MODULE PROVIDER>/<REGISTRY MODULE ID>)", | ||
d.Id(), | ||
) | ||
} | ||
registryModuleInfo := strings.SplitN(d.Id(), "/", 6) | ||
if len(registryModuleInfo) == 4 { | ||
// for format: <ORGANIZATION>/<REGISTRY MODULE NAME>/<REGISTRY MODULE PROVIDER>/<REGISTRY MODULE ID> | ||
log.Printf("[WARN] The import format <ORGANIZATION>/<REGISTRY MODULE NAME>/<REGISTRY MODULE PROVIDER>/<REGISTRY MODULE ID> is deprecated as of release 0.33.0 and may be removed in a future version. The preferred format is <ORGANIZATION>/<REGISTRY_NAME>/<NAMESPACE>/<REGISTRY MODULE NAME>/<REGISTRY MODULE PROVIDER>/<REGISTRY MODULE ID>.") | ||
d.Set("organization", registryModuleInfo[0]) | ||
d.Set("name", registryModuleInfo[1]) | ||
d.Set("module_provider", registryModuleInfo[2]) | ||
d.SetId(registryModuleInfo[3]) | ||
|
||
// Set the fields that are part of the import ID. | ||
d.Set("name", registryModuleInfo[1]) | ||
d.Set("module_provider", registryModuleInfo[2]) | ||
d.Set("organization", registryModuleInfo[0]) | ||
d.SetId(registryModuleInfo[3]) | ||
return []*schema.ResourceData{d}, nil | ||
} else if len(registryModuleInfo) == 6 { | ||
// for format: <ORGANIZATION>/<REGISTRY_NAME>/<NAMESPACE>/<REGISTRY MODULE NAME>/<REGISTRY MODULE PROVIDER>/<REGISTRY MODULE ID> | ||
// see https://www.terraform.io/cloud-docs/api-docs/private-registry/modules#get-a-module | ||
d.Set("organization", registryModuleInfo[0]) | ||
d.Set("registry_name", registryModuleInfo[1]) | ||
d.Set("namespace", registryModuleInfo[2]) | ||
d.Set("name", registryModuleInfo[3]) | ||
d.Set("module_provider", registryModuleInfo[4]) | ||
d.SetId(registryModuleInfo[5]) | ||
|
||
return []*schema.ResourceData{d}, nil | ||
} | ||
|
||
return []*schema.ResourceData{d}, nil | ||
return nil, fmt.Errorf( | ||
"invalid registry module import format: %s (expected <ORGANIZATION>/<REGISTRY_NAME>/<NAMESPACE>/<REGISTRY MODULE NAME>/<REGISTRY MODULE PROVIDER>/<REGISTRY MODULE ID>)", | ||
d.Id(), | ||
) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Pointing out
ForceNew
attribute for reviewing