Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
102 changes: 102 additions & 0 deletions examples/tenant_policies_endpoint_mac_tag_policy/main.tf
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 mso/datasource_mso_tenant_policies_endpoint_mac_tag_policy.go
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
}
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,
)
}
2 changes: 2 additions & 0 deletions mso/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ func Provider() terraform.ResourceProvider {
"mso_tenant_policies_ipsla_track_list": resourceMSOIPSLATrackList(),
"mso_tenant_policies_l3out_interface_routing_policy": resourceMSOL3OutInterfaceRoutingPolicy(),
"mso_fabric_policies_interface_setting": resourceMSOInterfaceSetting(),
"mso_tenant_policies_endpoint_mac_tag_policy": resourceMSOEndpointMACTagPolicy(),
},

DataSourcesMap: map[string]*schema.Resource{
Expand Down Expand Up @@ -217,6 +218,7 @@ func Provider() terraform.ResourceProvider {
"mso_tenant_policies_ipsla_track_list": datasourceMSOIPSLATrackList(),
"mso_tenant_policies_l3out_interface_routing_policy": datasourceMSOL3OutInterfaceRoutingPolicy(),
"mso_fabric_policies_interface_setting": datasourceMSOInterfaceSetting(),
"mso_tenant_policies_endpoint_mac_tag_policy": datasourceMSOEndpointMACTagPolicy(),
},

ConfigureFunc: configureClient,
Expand Down
Loading
Loading