-
Notifications
You must be signed in to change notification settings - Fork 6
Add Org networking settings vcfa_org_networking resource + data source #15
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
386ce1d
Add vcfa_org_networking resource + data source
Didainius 73294fd
Patch test, changelog
Didainius c5a8d9f
Self review, tests
Didainius 4925f9e
Latest govcd
Didainius 16ae54d
make static
Didainius c997b04
go mod
Didainius 691dc92
Merge branch 'main' into org-settings
Didainius 8e479f4
Address comments
Didainius File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,2 @@ | ||
| - **New Resource:** `vcfa_org_networking` to manage Org Networking Settings [GH-15] | ||
| - **New Data Source:** `vcfa_org_networking` to read Org Networking Settings [GH-15] |
This file contains hidden or 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 hidden or 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 hidden or 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,57 @@ | ||
| package vcfa | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
| ) | ||
|
|
||
| func datasourceVcfaOrgNetworking() *schema.Resource { | ||
| return &schema.Resource{ | ||
| ReadContext: datasourceVcfaOrgNetworkingRead, | ||
|
|
||
| Schema: map[string]*schema.Schema{ | ||
| "org_id": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| ForceNew: true, | ||
| Description: fmt.Sprintf("Parent %s for %s", labelVcfaOrg, labelVcfaOrgNetworking), | ||
| }, | ||
| "log_name": { | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| Description: fmt.Sprintf("A globally unique identifier (max 8 char) for this %s in the logs of the backing network provider", labelVcfaOrg), | ||
| }, | ||
| "networking_tenancy_enabled": { | ||
| Type: schema.TypeBool, | ||
| Computed: true, | ||
| Description: "Whether this Organization has tenancy for the network domain in the backing network provider", | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func datasourceVcfaOrgNetworkingRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
| vcfaClient := meta.(*VCDClient) | ||
| org, err := vcfaClient.GetTmOrgById(d.Get("org_id").(string)) | ||
| if err != nil { | ||
| return diag.Errorf("error retrieving %s: %s", labelVcfaOrg, err) | ||
| } | ||
|
|
||
| d.SetId(org.TmOrg.ID) | ||
| dSet(d, "org_id", org.TmOrg.ID) | ||
|
|
||
| orgNetworkingSettings, err := org.GetOrgNetworkingSettings() | ||
| if err != nil { | ||
| return diag.Errorf("error retrieving %s for %s:%s", labelVcfaOrgNetworking, labelVcfaOrg, err) | ||
| } | ||
|
|
||
| err = setOrgNetworkingSettingsData(vcfaClient, d, orgNetworkingSettings) | ||
| if err != nil { | ||
| return diag.Errorf("error storing read %s: %s", labelVcfaOrgNetworking, err) | ||
| } | ||
|
|
||
| return nil | ||
| } |
This file contains hidden or 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 hidden or 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,148 @@ | ||
| package vcfa | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" | ||
| "github.com/vmware/go-vcloud-director/v3/govcd" | ||
| "github.com/vmware/go-vcloud-director/v3/types/v56" | ||
| ) | ||
|
|
||
| const labelVcfaOrgNetworking = "Organization Networking Settings" | ||
|
|
||
| func resourceVcfaOrgNetworking() *schema.Resource { | ||
| return &schema.Resource{ | ||
| CreateContext: resourceVcfaOrgNetworkingCreateUpdate, | ||
| ReadContext: resourceVcfaOrgNetworkingRead, | ||
| UpdateContext: resourceVcfaOrgNetworkingCreateUpdate, | ||
| DeleteContext: resourceVcfaOrgNetworkingDelete, | ||
| Importer: &schema.ResourceImporter{ | ||
| StateContext: resourceVcfaOrgNetworkingImport, // The same as importing the Org | ||
| }, | ||
|
|
||
| Schema: map[string]*schema.Schema{ | ||
| "org_id": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| ForceNew: true, | ||
| Description: fmt.Sprintf("Parent %s for %s", labelVcfaOrg, labelVcfaOrgNetworking), | ||
| }, | ||
| "log_name": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| Description: fmt.Sprintf("A globally unique identifier (max 8 char) for this %s in the logs of the backing network provider", labelVcfaOrg), | ||
| ValidateDiagFunc: validation.ToDiagFunc(validation.StringLenBetween(0, 8)), | ||
| }, | ||
| "networking_tenancy_enabled": { | ||
| Type: schema.TypeBool, | ||
| Computed: true, | ||
| Description: fmt.Sprintf("Whether this %s has tenancy for the network domain in the backing network provider", labelVcfaOrg), | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func resourceVcfaOrgNetworkingCreateUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
| vcfaClient := meta.(*VCDClient) | ||
|
|
||
| org, err := vcfaClient.GetTmOrgById(d.Get("org_id").(string)) | ||
| if err != nil { | ||
| return diag.Errorf("error retrieving %s: %s", labelVcfaOrg, err) | ||
| } | ||
|
|
||
| d.SetId(org.TmOrg.ID) | ||
| dSet(d, "org_id", org.TmOrg.ID) | ||
|
|
||
| orgNetworkingSettings, err := getOrgNetworkingSettingsType(vcfaClient, d) | ||
| if err != nil { | ||
| return diag.FromErr(err) | ||
| } | ||
|
|
||
| _, err = org.UpdateOrgNetworkingSettings(orgNetworkingSettings) | ||
| if err != nil { | ||
| return diag.Errorf("error updating %s for %s:%s", labelVcfaOrgNetworking, labelVcfaOrg, err) | ||
| } | ||
|
|
||
| return resourceVcfaOrgNetworkingRead(ctx, d, meta) | ||
| } | ||
|
|
||
| func resourceVcfaOrgNetworkingRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
| vcfaClient := meta.(*VCDClient) | ||
| org, err := vcfaClient.GetTmOrgById(d.Get("org_id").(string)) | ||
| if err != nil { | ||
| return diag.Errorf("error retrieving %s: %s", labelVcfaOrg, err) | ||
| } | ||
|
|
||
| d.SetId(org.TmOrg.ID) | ||
| dSet(d, "org_id", org.TmOrg.ID) | ||
|
|
||
| orgNetworkingSettings, err := org.GetOrgNetworkingSettings() | ||
| if err != nil { | ||
| if govcd.ContainsNotFound(err) { // Org no longer present, removing from state | ||
| d.SetId("") | ||
| return nil | ||
| } | ||
| return diag.Errorf("error retrieving %s for %s:%s", labelVcfaOrgNetworking, labelVcfaOrg, err) | ||
| } | ||
|
|
||
| err = setOrgNetworkingSettingsData(vcfaClient, d, orgNetworkingSettings) | ||
| if err != nil { | ||
| return diag.Errorf("error storing read %s: %s", labelVcfaOrgNetworking, err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func resourceVcfaOrgNetworkingDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { | ||
| vcfaClient := meta.(*VCDClient) | ||
| org, err := vcfaClient.GetTmOrgById(d.Get("org_id").(string)) | ||
| if err != nil { | ||
| return diag.Errorf("error retrieving %s: %s", labelVcfaOrg, err) | ||
| } | ||
|
|
||
| // reset settings | ||
| resetSettings := &types.TmOrgNetworkingSettings{ | ||
| OrgNameForLogs: "", | ||
| } | ||
|
|
||
| _, err = org.UpdateOrgNetworkingSettings(resetSettings) | ||
| if err != nil { | ||
| return diag.Errorf("error removing Org Network Settings: %s", err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
|
|
||
| func resourceVcfaOrgNetworkingImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { | ||
| vcdClient := meta.(*VCDClient) | ||
|
|
||
| o, err := vcdClient.GetTmOrgByName(d.Id()) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("error getting Org: %s", err) | ||
| } | ||
|
|
||
| dSet(d, "org_id", o.TmOrg.ID) | ||
| d.SetId(o.TmOrg.ID) | ||
| return []*schema.ResourceData{d}, nil | ||
| } | ||
|
|
||
| func getOrgNetworkingSettingsType(_ *VCDClient, d *schema.ResourceData) (*types.TmOrgNetworkingSettings, error) { | ||
| t := &types.TmOrgNetworkingSettings{ | ||
| OrgNameForLogs: d.Get("log_name").(string), | ||
| // No setting for it in UI | ||
| // NetworkingTenancyEnabled: addrOf(d.Get("networking_tenancy_enabled").(bool)), | ||
| } | ||
|
|
||
| return t, nil | ||
| } | ||
|
|
||
| func setOrgNetworkingSettingsData(_ *VCDClient, d *schema.ResourceData, orgNetConfig *types.TmOrgNetworkingSettings) error { | ||
| dSet(d, "log_name", orgNetConfig.OrgNameForLogs) | ||
| if orgNetConfig.NetworkingTenancyEnabled != nil { | ||
| dSet(d, "networking_tenancy_enabled", *orgNetConfig.NetworkingTenancyEnabled) | ||
| } | ||
| return nil | ||
| } | ||
This file contains hidden or 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,129 @@ | ||
| //go:build org || tm || ALL || functional | ||
|
|
||
| package vcfa | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource" | ||
| ) | ||
|
|
||
| func TestAccVcfaOrgNetworking(t *testing.T) { | ||
| preTestChecks(t) | ||
| skipIfNotSysAdmin(t) | ||
|
|
||
| var params = StringMap{ | ||
| "Testname": t.Name(), | ||
| "Tags": "tm org", | ||
| } | ||
| testParamsNotEmpty(t, params) | ||
|
|
||
| configText1 := templateFill(testAccVcfaOrgNetworkingStep1, params) | ||
| params["FuncName"] = t.Name() + "-step2" | ||
| configText2 := templateFill(testAccVcfaOrgNetworkingStep2, params) | ||
| params["FuncName"] = t.Name() + "-step3" | ||
| configText3 := templateFill(testAccVcfaOrgNetworkingStep3, params) | ||
| params["FuncName"] = t.Name() + "-step4" | ||
| configText4 := templateFill(testAccVcfaOrgNetworkingStep4DS, params) | ||
|
|
||
| debugPrintf("#[DEBUG] CONFIGURATION step1: %s\n", configText1) | ||
| debugPrintf("#[DEBUG] CONFIGURATION step2: %s\n", configText2) | ||
| debugPrintf("#[DEBUG] CONFIGURATION step3: %s\n", configText3) | ||
| debugPrintf("#[DEBUG] CONFIGURATION step4: %s\n", configText4) | ||
| if vcfaShortTest { | ||
| t.Skip(acceptanceTestsSkipped) | ||
| return | ||
| } | ||
|
|
||
| resource.Test(t, resource.TestCase{ | ||
| ProviderFactories: testAccProviders, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| Config: configText1, | ||
| Check: resource.ComposeTestCheckFunc( | ||
| resource.TestCheckResourceAttr("vcfa_org.test", "name", t.Name()), | ||
| resource.TestCheckResourceAttrPair("vcfa_org.test", "id", "vcfa_org_networking.test", "id"), | ||
| resource.TestCheckResourceAttr("vcfa_org_networking.test", "log_name", "l-one"), | ||
| ), | ||
| }, | ||
| { | ||
| Config: configText2, | ||
| Check: resource.ComposeTestCheckFunc( | ||
| resource.TestCheckResourceAttr("vcfa_org.test", "name", t.Name()), | ||
| resource.TestCheckResourceAttrPair("vcfa_org.test", "id", "vcfa_org_networking.test", "id"), | ||
| resource.TestCheckResourceAttr("vcfa_org_networking.test", "log_name", ""), | ||
| ), | ||
| }, | ||
| { | ||
| Config: configText3, | ||
| Check: resource.ComposeTestCheckFunc( | ||
| resource.TestCheckResourceAttr("vcfa_org.test", "name", t.Name()+""), | ||
| resource.TestCheckResourceAttrPair("vcfa_org.test", "id", "vcfa_org_networking.test", "id"), | ||
| resource.TestCheckResourceAttr("vcfa_org_networking.test", "log_name", "l-one-u"), | ||
| ), | ||
| }, | ||
| { | ||
| ResourceName: "vcfa_org_networking.test", | ||
| ImportState: true, | ||
| ImportStateVerify: true, | ||
| ImportStateId: params["Testname"].(string), // Org name | ||
| }, | ||
| { | ||
| Config: configText4, | ||
| Check: resource.ComposeTestCheckFunc( | ||
| resourceFieldsEqual("vcfa_org_networking.test", "data.vcfa_org_networking.test", nil), | ||
| ), | ||
| }, | ||
| }, | ||
| }) | ||
|
|
||
| postTestChecks(t) | ||
| } | ||
|
|
||
| const testAccVcfaOrgNetworkingStep1 = ` | ||
| resource "vcfa_org" "test" { | ||
| name = "{{.Testname}}" | ||
| display_name = "terraform-test" | ||
| description = "terraform test" | ||
| is_enabled = true | ||
| } | ||
|
|
||
| resource "vcfa_org_networking" "test" { | ||
| org_id = vcfa_org.test.id | ||
| log_name = "l-one" | ||
| } | ||
| ` | ||
|
|
||
| const testAccVcfaOrgNetworkingStep2 = ` | ||
| resource "vcfa_org" "test" { | ||
| name = "{{.Testname}}" | ||
| display_name = "terraform-test" | ||
| description = "" | ||
| is_enabled = true | ||
| } | ||
|
|
||
| resource "vcfa_org_networking" "test" { | ||
| org_id = vcfa_org.test.id | ||
| log_name = "" | ||
| } | ||
| ` | ||
|
|
||
| const testAccVcfaOrgNetworkingStep3 = ` | ||
| resource "vcfa_org" "test" { | ||
| name = "{{.Testname}}" | ||
| display_name = "terraform-test" | ||
| description = "" | ||
| is_enabled = true | ||
| } | ||
|
|
||
| resource "vcfa_org_networking" "test" { | ||
| org_id = vcfa_org.test.id | ||
| log_name = "l-one-u" | ||
| } | ||
| ` | ||
|
|
||
| const testAccVcfaOrgNetworkingStep4DS = testAccVcfaOrgNetworkingStep3 + ` | ||
| data "vcfa_org_networking" "test" { | ||
| org_id = vcfa_org.test.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.
Uh oh!
There was an error while loading. Please reload this page.