Skip to content

Commit 93981ff

Browse files
committed
[minor_change] Addition of resource and data source for BGP Peer Prefix Policy under tenant policies
1 parent 67646e1 commit 93981ff

8 files changed

+693
-0
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
terraform {
2+
required_providers {
3+
mso = {
4+
source = "CiscoDevNet/mso"
5+
}
6+
}
7+
}
8+
9+
provider "mso" {
10+
username = "" # <MSO username>
11+
password = "" # <MSO pwd>
12+
url = "" # <MSO URL>
13+
insecure = true
14+
}
15+
16+
data "mso_tenant" "example_tenant" {
17+
name = "example_tenant"
18+
}
19+
20+
# tenant template example
21+
22+
resource "mso_template" "tenant_template" {
23+
template_name = "tenant_template"
24+
template_type = "tenant"
25+
tenant_id = data.mso_tenant.example_tenant.id
26+
}
27+
28+
# tenant policies bgp peer prefix policy example
29+
30+
resource "mso_tenant_policies_bgp_peer_prefix_policy" "bgp_policy" {
31+
template_id = mso_template.tenant_template.id
32+
name = "test_bgp_peer_prefix_policy"
33+
description = "Test BGP Peer Prefix Policy"
34+
action = "restart"
35+
max_number_of_prefixes = 1000
36+
threshold_percentage = 50
37+
restart_time = 60
38+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package mso
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/ciscoecosystem/mso-go-client/client"
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
9+
)
10+
11+
func datasourceMSOBGPPeerPrefixPolicy() *schema.Resource {
12+
return &schema.Resource{
13+
Read: dataSourceMSOBGPPeerPrefixPolicyRead,
14+
15+
Schema: map[string]*schema.Schema{
16+
"template_id": {
17+
Type: schema.TypeString,
18+
Required: true,
19+
Description: "The ID of the tenant policy template.",
20+
},
21+
"name": {
22+
Type: schema.TypeString,
23+
Required: true,
24+
Description: "The name of the BGP Peer Prefix Policy.",
25+
},
26+
"uuid": {
27+
Type: schema.TypeString,
28+
Computed: true,
29+
Description: "The UUID of the BGP Peer Prefix Policy.",
30+
},
31+
"description": {
32+
Type: schema.TypeString,
33+
Computed: true,
34+
Description: "The description of the BGP Peer Prefix Policy.",
35+
},
36+
"action": {
37+
Type: schema.TypeString,
38+
Computed: true,
39+
Description: "The action of the BGP Peer Prefix Policy (log, reject, restart, shutdown).",
40+
},
41+
"max_number_of_prefixes": {
42+
Type: schema.TypeInt,
43+
Computed: true,
44+
Description: "The maximum number of prefixes for the BGP Peer Prefix Policy.",
45+
},
46+
"threshold_percentage": {
47+
Type: schema.TypeInt,
48+
Computed: true,
49+
Description: "The threshold percentage of the BGP Peer Prefix Policy.",
50+
},
51+
"restart_time": {
52+
Type: schema.TypeInt,
53+
Computed: true,
54+
Description: "The restart time of the BGP Peer Prefix Policy in seconds.",
55+
},
56+
},
57+
}
58+
}
59+
60+
func dataSourceMSOBGPPeerPrefixPolicyRead(d *schema.ResourceData, m interface{}) error {
61+
log.Printf("[DEBUG] MSO BGP Peer Prefix Policy Data Source - Beginning Read")
62+
msoClient := m.(*client.Client)
63+
64+
templateId := d.Get("template_id").(string)
65+
policyName := d.Get("name").(string)
66+
67+
response, err := msoClient.GetViaURL(fmt.Sprintf("api/v1/templates/%s", templateId))
68+
if err != nil {
69+
return err
70+
}
71+
72+
policy, err := GetPolicyByName(response, policyName, "tenantPolicyTemplate", "template", "bgpPeerPrefixPolicies")
73+
if err != nil {
74+
return err
75+
}
76+
77+
setBGPPeerPrefixPolicyData(d, policy, templateId)
78+
log.Printf("[DEBUG] MSO BGP Peer Prefix Policy Data Source - Read Complete: %v", d.Id())
79+
return nil
80+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package mso
2+
3+
import (
4+
"fmt"
5+
"testing"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
8+
)
9+
10+
func TestAccMSOTenantPoliciesBGPPeerPrefixPolicyDataSource(t *testing.T) {
11+
resource.Test(t, resource.TestCase{
12+
PreCheck: func() { testAccPreCheck(t) },
13+
Providers: testAccProviders,
14+
Steps: []resource.TestStep{
15+
{
16+
PreConfig: func() { fmt.Println("Test: BGP Peer Prefix Policy Data Source") },
17+
Config: testAccMSOTenantPoliciesBGPPeerPrefixPolicyDataSource(),
18+
Check: resource.ComposeTestCheckFunc(
19+
resource.TestCheckResourceAttr("data.mso_tenant_policies_bgp_peer_prefix_policy.bgp_policy", "name", "test_bgp_peer_prefix_policy"),
20+
resource.TestCheckResourceAttr("data.mso_tenant_policies_bgp_peer_prefix_policy.bgp_policy", "description", "Test BGP Peer Prefix Policy"),
21+
resource.TestCheckResourceAttr("data.mso_tenant_policies_bgp_peer_prefix_policy.bgp_policy", "action", "restart"),
22+
resource.TestCheckResourceAttr("data.mso_tenant_policies_bgp_peer_prefix_policy.bgp_policy", "max_number_of_prefixes", "1000"),
23+
resource.TestCheckResourceAttr("data.mso_tenant_policies_bgp_peer_prefix_policy.bgp_policy", "threshold_percentage", "50"),
24+
resource.TestCheckResourceAttr("data.mso_tenant_policies_bgp_peer_prefix_policy.bgp_policy", "restart_time", "60"),
25+
),
26+
},
27+
},
28+
})
29+
}
30+
31+
func testAccMSOTenantPoliciesBGPPeerPrefixPolicyDataSource() string {
32+
return fmt.Sprintf(`%s
33+
data "mso_tenant_policies_bgp_peer_prefix_policy" "bgp_policy" {
34+
template_id = mso_tenant_policies_bgp_peer_prefix_policy.bgp_policy.template_id
35+
name = "test_bgp_peer_prefix_policy"
36+
}`, testAccMSOTenantPoliciesBGPPeerPrefixPolicyConfigCreate())
37+
}

mso/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,6 +133,7 @@ func Provider() terraform.ResourceProvider {
133133
"mso_fabric_policies_synce_interface_policy": resourceMSOSyncEInterfacePolicy(),
134134
"mso_fabric_policies_macsec_policy": resourceMSOMacsecPolicy(),
135135
"mso_schema_template_contract_service_chaining": resourceMSOSchemaTemplateContractServiceChaining(),
136+
"mso_tenant_policies_bgp_peer_prefix_policy": resourceMSOBGPPeerPrefixPolicy(),
136137
},
137138

138139
DataSourcesMap: map[string]*schema.Resource{
@@ -199,6 +200,7 @@ func Provider() terraform.ResourceProvider {
199200
"mso_fabric_policies_synce_interface_policy": datasourceMSOSyncEInterfacePolicy(),
200201
"mso_fabric_policies_macsec_policy": datasourceMacsecPolicy(),
201202
"mso_schema_template_contract_service_chaining": datasourceMSOSchemaTemplateContractServiceChaining(),
203+
"mso_tenant_policies_bgp_peer_prefix_policy": datasourceMSOBGPPeerPrefixPolicy(),
202204
},
203205

204206
ConfigureFunc: configureClient,

0 commit comments

Comments
 (0)