-
Notifications
You must be signed in to change notification settings - Fork 33
Add mso_tenant_policies_endpoint_mac_tag_policy resource and datasource (DCNE-652) #460
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
Open
sajagana
wants to merge
4
commits into
CiscoDevNet:master
Choose a base branch
from
sajagana:426_endpoint_mac_tag_policy
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
a98cbf6
[minor_change] Add mso_tenant_policies_endpoint_mac_tag_policy resour…
sajagana 20ebec6
[ignore] Removed ForceNew from the mac attribute
sajagana 74ac6c6
[ignore] Remove resource level input validation
sajagana 3171dc5
[ignore] Update the tenant_policies_endpoint_mac_tag_policy resource …
sajagana 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
102 changes: 102 additions & 0 deletions
102
examples/tenant_policies_endpoint_mac_tag_policy/main.tf
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,102 @@ | ||
| terraform { | ||
| required_providers { | ||
| mso = { | ||
| source = "CiscoDevNet/mso" | ||
| } | ||
| } | ||
| } | ||
|
|
||
| provider "mso" { | ||
| username = "" # <MSO username> | ||
| password = "" # <MSO pwd> | ||
| url = "" # <MSO URL> | ||
| insecure = true | ||
| } | ||
|
|
||
| resource "mso_tenant" "tf_tenant" { | ||
| name = "tf_tenant" | ||
| display_name = "tf_tenant" | ||
| } | ||
|
|
||
| resource "mso_schema" "tf_schema" { | ||
| name = "tf_schema" | ||
| template { | ||
| name = "tf_template" | ||
| display_name = "tf_template" | ||
| tenant_id = mso_tenant.tf_tenant.id | ||
| } | ||
| } | ||
|
|
||
| resource "mso_schema_template_vrf" "tf_vrf" { | ||
| name = "tf_vrf" | ||
| display_name = "tf_vrf" | ||
| schema_id = mso_schema.tf_schema.id | ||
| template = one(mso_schema.tf_schema.template).name | ||
| } | ||
|
|
||
| resource "mso_schema_template_bd" "tf_bd" { | ||
| schema_id = mso_schema.tf_schema.id | ||
| template_name = one(mso_schema.tf_schema.template).name | ||
| name = "tf_bd" | ||
| display_name = "tf_bd" | ||
| layer2_unknown_unicast = "proxy" | ||
| vrf_name = mso_schema_template_vrf.tf_vrf.name | ||
| } | ||
|
|
||
| resource "mso_template" "tf_tenant_template" { | ||
| template_name = "tf_tenant_template" | ||
| template_type = "tenant" | ||
| tenant_id = mso_tenant.tf_tenant.id | ||
| } | ||
|
|
||
| resource "mso_tenant_policies_endpoint_mac_tag_policy" "endpoint_mac_tag_bd" { | ||
| template_id = mso_template.tf_tenant_template.id | ||
| mac = "AA:BB:A1:B2:C3:D4" | ||
| bd_uuid = mso_schema_template_bd.tf_bd.uuid | ||
|
|
||
| tag_annotations { | ||
| key = "annotation_key_1" | ||
| value = "annotation_value_1" | ||
| } | ||
|
|
||
| tag_annotations { | ||
| key = "annotation_key_2" | ||
| value = "annotation_value_2" | ||
| } | ||
|
|
||
| policy_tags { | ||
| key = "policy_key_1" | ||
| value = "policy_value_1" | ||
| } | ||
|
|
||
| policy_tags { | ||
| key = "policy_key_2" | ||
| value = "policy_value_2" | ||
| } | ||
| } | ||
|
|
||
| resource "mso_tenant_policies_endpoint_mac_tag_policy" "endpoint_mac_tag_vrf" { | ||
| template_id = mso_template.tf_tenant_template.id | ||
| mac = "AA:BB:A1:B2:C3:D4" | ||
| vrf_uuid = mso_schema_template_vrf.tf_vrf.uuid | ||
|
|
||
| tag_annotations { | ||
| key = "annotation_key_1" | ||
| value = "annotation_value_1" | ||
| } | ||
|
|
||
| tag_annotations { | ||
| key = "annotation_key_2" | ||
| value = "annotation_value_2" | ||
| } | ||
|
|
||
| policy_tags { | ||
| key = "policy_key_1" | ||
| value = "policy_value_1" | ||
| } | ||
|
|
||
| policy_tags { | ||
| key = "policy_key_2" | ||
| value = "policy_value_2" | ||
| } | ||
| } |
115 changes: 115 additions & 0 deletions
115
mso/datasource_mso_tenant_policies_endpoint_mac_tag_policy.go
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,115 @@ | ||
| package mso | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "log" | ||
|
|
||
| "github.com/ciscoecosystem/mso-go-client/client" | ||
| "github.com/hashicorp/terraform-plugin-sdk/helper/schema" | ||
| ) | ||
|
|
||
| func datasourceMSOEndpointMACTagPolicy() *schema.Resource { | ||
| return &schema.Resource{ | ||
| Read: dataSourceMSOEndpointMACTagPolicyRead, | ||
|
|
||
| Schema: map[string]*schema.Schema{ | ||
| "template_id": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| }, | ||
| "mac": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| }, | ||
| "bd_uuid": { | ||
| Type: schema.TypeString, | ||
| Optional: true, | ||
| ConflictsWith: []string{"vrf_uuid"}, | ||
| }, | ||
| "vrf_uuid": { | ||
| Type: schema.TypeString, | ||
| Optional: true, | ||
| ConflictsWith: []string{"bd_uuid"}, | ||
| }, | ||
| "name": { | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| }, | ||
| "uuid": { | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| }, | ||
| "tag_annotations": { | ||
| Type: schema.TypeList, | ||
| Computed: true, | ||
| Elem: &schema.Resource{ | ||
| Schema: map[string]*schema.Schema{ | ||
| "key": { | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| }, | ||
| "value": { | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| "policy_tags": { | ||
| Type: schema.TypeList, | ||
| Computed: true, | ||
| Elem: &schema.Resource{ | ||
| Schema: map[string]*schema.Schema{ | ||
| "key": { | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| }, | ||
| "value": { | ||
| Type: schema.TypeString, | ||
| Computed: true, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| }, | ||
| } | ||
| } | ||
|
|
||
| func dataSourceMSOEndpointMACTagPolicyRead(d *schema.ResourceData, m interface{}) error { | ||
| log.Printf("[DEBUG] MSO Endpoint MAC Tag Policy Data Source - Beginning Read") | ||
| msoClient := m.(*client.Client) | ||
|
|
||
| templateId := d.Get("template_id").(string) | ||
| mac := d.Get("mac").(string) | ||
| bdUUID := d.Get("bd_uuid").(string) | ||
| vrfUUID := d.Get("vrf_uuid").(string) | ||
|
|
||
| // Either 'bd_uuid' or 'vrf_uuid' must be specified to format the data source ID. | ||
| if bdUUID == "" && vrfUUID == "" { | ||
| return fmt.Errorf("Either 'bd_uuid' or 'vrf_uuid' must be specified to use Endpoint MAC Tag Policy Data Source") | ||
| } | ||
|
|
||
| name, dataSourceId, err := setEndpointMACTagPolicyId(m, templateId, "", mac, bdUUID, vrfUUID) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| d.SetId(dataSourceId) | ||
|
|
||
| response, err := msoClient.GetViaURL(fmt.Sprintf("api/v1/templates/%s", templateId)) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| policy, err := GetPolicyByName(response, name, "tenantPolicyTemplate", "template", "endpointMacTagPolicies") | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| err = setEndpointMACTagPolicyData(d, policy, templateId, m) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| log.Printf("[DEBUG] MSO Endpoint MAC Tag Policy Data Source - Read Complete : %v", d.Id()) | ||
| return nil | ||
| } | ||
96 changes: 96 additions & 0 deletions
96
mso/datasource_mso_tenant_policies_endpoint_mac_tag_policy_test.go
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,96 @@ | ||
| package mso | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "regexp" | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
| ) | ||
|
|
||
| func TestAccMSOTenantPoliciesEndpointMACTagPolicyDataSource(t *testing.T) { | ||
| resource.Test(t, resource.TestCase{ | ||
| PreCheck: func() { testAccPreCheck(t) }, | ||
| Providers: testAccProviders, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| PreConfig: func() { fmt.Println("Test: Error when neither bd_uuid nor vrf_uuid is provided Data Source") }, | ||
| Config: testAccMSOTenantPoliciesEndpointMACTagPolicyConfigErrorMissingScopeDataSource(), | ||
| ExpectError: regexp.MustCompile(`Either 'bd_uuid' or 'vrf_uuid' must be specified to use Endpoint MAC Tag Policy Data Source`), | ||
| }, | ||
| { | ||
| PreConfig: func() { fmt.Println("Test: Error when both bd_uuid and vrf_uuid are provided Data Source") }, | ||
| Config: testAccMSOTenantPoliciesEndpointMACTagPolicyConfigErrorConflictingScopeDataSource(), | ||
| ExpectError: regexp.MustCompile(`conflicts with`), | ||
| }, | ||
| { | ||
| PreConfig: func() { | ||
| fmt.Println("Test: Endpoint MAC Tag Policy (BD scope) with multiple annotations and tags Data Source") | ||
| }, | ||
| Config: testAccMSOTenantPoliciesEndpointMACTagPolicyConfigCreateBDWithMultipleTagsDataSource(), | ||
| Check: resource.ComposeAggregateTestCheckFunc( | ||
| resource.TestCheckResourceAttr("data.mso_tenant_policies_endpoint_mac_tag_policy.endpoint_mac_bd", "mac", "AA:BB:A1:B2:C3:D4"), | ||
| resource.TestCheckResourceAttr("data.mso_tenant_policies_endpoint_mac_tag_policy.endpoint_mac_bd", "name", fmt.Sprintf("AA:BB:A1:B2:C3:D4-[%s]", msoSchemaTemplateBdName)), | ||
| resource.TestCheckResourceAttrSet("data.mso_tenant_policies_endpoint_mac_tag_policy.endpoint_mac_bd", "uuid"), | ||
| resource.TestCheckResourceAttrSet("data.mso_tenant_policies_endpoint_mac_tag_policy.endpoint_mac_bd", "bd_uuid"), | ||
| resource.TestCheckResourceAttr("data.mso_tenant_policies_endpoint_mac_tag_policy.endpoint_mac_bd", "tag_annotations.#", "2"), | ||
| resource.TestCheckResourceAttr("data.mso_tenant_policies_endpoint_mac_tag_policy.endpoint_mac_bd", "policy_tags.#", "2"), | ||
| CustomTestCheckTypeSetElemAttrs("data.mso_tenant_policies_endpoint_mac_tag_policy.endpoint_mac_bd", "tag_annotations", | ||
| map[string]string{ | ||
| "key": "annotation_key_1", | ||
| "value": "annotation_value_1", | ||
| }, | ||
| ), | ||
| CustomTestCheckTypeSetElemAttrs("data.mso_tenant_policies_endpoint_mac_tag_policy.endpoint_mac_bd", "tag_annotations", | ||
| map[string]string{ | ||
| "key": "annotation_key_2", | ||
| "value": "annotation_value_2", | ||
| }, | ||
| ), | ||
| CustomTestCheckTypeSetElemAttrs("data.mso_tenant_policies_endpoint_mac_tag_policy.endpoint_mac_bd", "policy_tags", | ||
| map[string]string{ | ||
| "key": "policy_key_1", | ||
| "value": "policy_value_1", | ||
| }, | ||
| ), | ||
| CustomTestCheckTypeSetElemAttrs("data.mso_tenant_policies_endpoint_mac_tag_policy.endpoint_mac_bd", "policy_tags", | ||
| map[string]string{ | ||
| "key": "policy_key_2", | ||
| "value": "policy_value_2", | ||
| }, | ||
| ), | ||
| ), | ||
| }, | ||
| }, | ||
| }) | ||
| } | ||
|
|
||
| func testAccMSOTenantPoliciesEndpointMACTagPolicyConfigErrorMissingScopeDataSource() string { | ||
| return ` | ||
| data "mso_tenant_policies_endpoint_mac_tag_policy" "error" { | ||
| template_id = "mso_template.error.id" | ||
| mac = "AA:BB:A1:B2:C3:D4" | ||
| }` | ||
| } | ||
|
|
||
| func testAccMSOTenantPoliciesEndpointMACTagPolicyConfigErrorConflictingScopeDataSource() string { | ||
| return ` | ||
| data "mso_tenant_policies_endpoint_mac_tag_policy" "error" { | ||
| template_id = "mso_template.error.id" | ||
| mac = "AA:BB:A1:B2:C3:D4" | ||
| bd_uuid = "mso_schema_template_bd.error.uuid" | ||
| vrf_uuid = "mso_schema_template_vrf.error.uuid" | ||
| }` | ||
| } | ||
|
|
||
| func testAccMSOTenantPoliciesEndpointMACTagPolicyConfigCreateBDWithMultipleTagsDataSource() string { | ||
| return fmt.Sprintf(`%[1]s | ||
| data "mso_tenant_policies_endpoint_mac_tag_policy" "endpoint_mac_bd" { | ||
| template_id = mso_template.%[2]s.id | ||
| mac = "AA:BB:A1:B2:C3:D4" | ||
| bd_uuid = mso_tenant_policies_endpoint_mac_tag_policy.endpoint_mac_bd.bd_uuid | ||
| }`, | ||
| testAccMSOTenantPoliciesEndpointMACTagPolicyConfigCreateBDWithMultipleTags(), | ||
| msoTenantPolicyTemplateName, | ||
| ) | ||
| } |
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
Oops, something went wrong.
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.