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
32 changes: 32 additions & 0 deletions examples/tenant_policies_route_map_policy_route_control/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
terraform {
required_providers {
mso = {
source = "CiscoDevNet/mso"
}
}
}

provider "mso" {
username = "" # <MSO username>
password = "" # <MSO pwd>
url = "" # <MSO URL>
insecure = true
}

data "mso_tenant" "example_tenant" {
name = "example_tenant"
}

# tenant template example

resource "mso_template" "tenant_template" {
template_name = "tenant_template"
template_type = "tenant"
tenant_id = data.mso_tenant.example_tenant.id
}

resource "mso_tenant_policies_route_map_policy_route_control" "route_map_policy" {
template_id = mso_template.tenant_template.id
name = "example_route_map_policy"
description = "Example Route Map Policy"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
terraform {
required_providers {
mso = {
source = "CiscoDevNet/mso"
}
}
}

provider "mso" {
username = "" # <MSO username>
password = "" # <MSO pwd>
url = "" # <MSO URL>
insecure = true
}

data "mso_tenant" "example_tenant" {
name = "example_tenant"
}

# tenant template example

resource "mso_template" "tenant_template" {
template_name = "tenant_template"
template_type = "tenant"
tenant_id = data.mso_tenant.example_tenant.id
}

resource "mso_tenant_policies_route_map_policy_route_control" "route_map_policy" {
template_id = mso_template.tenant_template.id
name = "example_route_map_policy"
description = "Example Route Map Policy"
}

resource "mso_tenant_policies_route_map_policy_route_control_context" "context" {
parent_id = mso_tenant_policies_route_map_policy_route_control.route_map_policy.id
name = "example_context"
description = "Example Route Control Context"
order = 1
action = "permit"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package mso

import (
"fmt"
"log"

"github.com/ciscoecosystem/mso-go-client/client"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func datasourceMSORouteMapPolicy() *schema.Resource {
return &schema.Resource{
Read: dataSourceMSORouteMapPolicyRead,

Schema: map[string]*schema.Schema{
"template_id": {
Type: schema.TypeString,
Required: true,
Description: "The ID of the tenant policy template.",
},
"name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the Route Map Policy.",
},
"uuid": {
Type: schema.TypeString,
Computed: true,
Description: "The UUID of the Route Map Policy.",
},
"description": {
Type: schema.TypeString,
Computed: true,
Description: "The description of the Route Map Policy.",
},
},
}
}

func dataSourceMSORouteMapPolicyRead(d *schema.ResourceData, m interface{}) error {
log.Printf("[DEBUG] MSO Route Map Policy Data Source - Beginning Read")
msoClient := m.(*client.Client)

templateId := d.Get("template_id").(string)
policyName := d.Get("name").(string)

response, err := msoClient.GetViaURL(fmt.Sprintf("api/v1/templates/%s", templateId))
if err != nil {
return err
}

policy, err := GetPolicyByName(response, policyName, "tenantPolicyTemplate", "template", "routeMapPolicies")
if err != nil {
return err
}

setRouteMapPolicyData(d, policy, templateId)

log.Printf("[DEBUG] MSO Route Map Policy Data Source - Read Complete: %v", d.Id())
return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package mso

import (
"fmt"
"log"

"github.com/ciscoecosystem/mso-go-client/client"
"github.com/ciscoecosystem/mso-go-client/container"
"github.com/ciscoecosystem/mso-go-client/models"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func datasourceMSORouteMapPolicyContext() *schema.Resource {
return &schema.Resource{
Read: dataSourceMSORouteMapPolicyContextRead,

Schema: map[string]*schema.Schema{
"parent_id": {
Type: schema.TypeString,
Required: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"action": {
Type: schema.TypeString,
Computed: true,
},
"order": {
Type: schema.TypeInt,
Computed: true,
},
"match_rule_uuids": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"set_rule_uuid": {
Type: schema.TypeString,
Computed: true,
},
},
}
}

func dataSourceMSORouteMapPolicyContextRead(d *schema.ResourceData, m interface{}) error {
log.Printf("[DEBUG] MSO Route Map Policy Context Data Source - Beginning Read")
msoClient := m.(*client.Client)
parentId := d.Get("parent_id").(string)
contextName := d.Get("name").(string)

templateId, err := GetTemplateIdFromResourceId(parentId)
if err != nil {
return err
}
policyName, err := GetPolicyNameFromResourceId(parentId, "RouteMapPolicy")
if err != nil {
return err
}

response, err := msoClient.GetViaURL(fmt.Sprintf("api/v1/templates/%s", templateId))
if err != nil {
return err
}

policy, err := GetPolicyByName(response, policyName, "tenantPolicyTemplate", "template", "routeMapPolicies")
if err != nil {
return err
}

contexts := policy.S("contexts")
count, _ := contexts.ArrayCount()

var match *container.Container
for i := 0; i < count; i++ {
c := contexts.Index(i)
if models.StripQuotes(c.S("name").String()) == contextName {
match = c
break
}
}

if match == nil {
return fmt.Errorf("Route Map Policy Context '%s' not found in parent policy '%s'", contextName, policyName)
}

d.SetId(fmt.Sprintf("%s/context/%s", parentId, models.StripQuotes(match.S("name").String())))
d.Set("parent_id", parentId)
d.Set("name", contextName)

if match.Exists("description") {
descValue := models.StripQuotes(match.S("description").String())
if descValue == "{}" {
d.Set("description", "")
} else {
d.Set("description", descValue)
}
} else {
d.Set("description", "")
}

if match.Exists("order") {
d.Set("order", int(match.S("order").Data().(float64)))
}

if match.Exists("action") {
d.Set("action", models.StripQuotes(match.S("action").String()))
}

if match.Exists("setRuleRef") {
setRuleValue := models.StripQuotes(match.S("setRuleRef").String())
if setRuleValue == "{}" {
d.Set("set_rule_uuid", "")
} else {
d.Set("set_rule_uuid", setRuleValue)
}
} else {
d.Set("set_rule_uuid", "")
}

if match.Exists("matchRules") {
apiList, _ := match.S("matchRules").Data().([]interface{})
uuids := make([]string, len(apiList))
for i, uuid := range apiList {
uuids[i] = models.StripQuotes(fmt.Sprintf("%v", uuid))
}
d.Set("match_rule_uuids", uuids)
} else {
d.Set("match_rule_uuids", []string{})
}

log.Printf("[DEBUG] MSO Route Map Policy Context Data Source - Read Complete: %v", d.Id())
return nil
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
package mso

import (
"fmt"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
)

func TestAccMSORouteMapPolicyContextDataSource(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
PreConfig: func() { fmt.Println("Task: Create Resource and Query via Data Source") },
Config: testAccMSORouteMapPolicyContextDataSourceConfig(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttrPair(
"data.mso_tenant_policies_route_map_policy_route_control_context.test", "parent_id",
"mso_tenant_policies_route_map_policy_route_control_context.test", "parent_id",
),
resource.TestCheckResourceAttrPair(
"data.mso_tenant_policies_route_map_policy_route_control_context.test", "name",
"mso_tenant_policies_route_map_policy_route_control_context.test", "name",
),
resource.TestCheckResourceAttrPair(
"data.mso_tenant_policies_route_map_policy_route_control_context.test", "description",
"mso_tenant_policies_route_map_policy_route_control_context.test", "description",
),
resource.TestCheckResourceAttrPair(
"data.mso_tenant_policies_route_map_policy_route_control_context.test", "action",
"mso_tenant_policies_route_map_policy_route_control_context.test", "action",
),
resource.TestCheckResourceAttrPair(
"data.mso_tenant_policies_route_map_policy_route_control_context.test", "order",
"mso_tenant_policies_route_map_policy_route_control_context.test", "order",
),
resource.TestCheckResourceAttrPair(
"data.mso_tenant_policies_route_map_policy_route_control_context.test", "set_rule_uuid",
"mso_tenant_policies_route_map_policy_route_control_context.test", "set_rule_uuid",
),
resource.TestCheckResourceAttrPair(
"data.mso_tenant_policies_route_map_policy_route_control_context.test", "match_rules.#",
"mso_tenant_policies_route_map_policy_route_control_context.test", "match_rules.#",
),
),
},
},
})
}

func testAccMSORouteMapPolicyContextDataSourceConfig() string {
return fmt.Sprintf(`%s
resource "mso_tenant_policies_route_map_policy_route_control" "test" {
template_id = mso_template.template_tenant.id
name = "test_route_map_ds"
description = "Route Map for data source test"
}

resource "mso_tenant_policies_route_map_policy_route_control_context" "test" {
parent_id = mso_tenant_policies_route_map_policy_route_control.test.id
name = "ctx_ds_1"
description = "Context for data source test"
action = "permit"
order = 1
}

data "mso_tenant_policies_route_map_policy_route_control_context" "test" {
parent_id = mso_tenant_policies_route_map_policy_route_control_context.test.parent_id
name = "ctx_ds_1"
}
`, testAccMSOTemplateResourceTenantConfig())
}
Loading
Loading