Skip to content

Commit

Permalink
[minor_change] Addition of new resource and data source for mso_tenan…
Browse files Browse the repository at this point in the history
…t_policies_ipsla_monitoring_policy
  • Loading branch information
shrsr committed Jan 29, 2025
1 parent 7dafa77 commit ae7c8b7
Show file tree
Hide file tree
Showing 11 changed files with 940 additions and 12 deletions.
53 changes: 53 additions & 0 deletions examples/tenant_policies_ipsla_monitoring_policy/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
terraform {
required_providers {
mso = {
source = "CiscoDevNet/mso"
}
}
}

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

data "mso_site" "site_1" {
name = "example_site_1"
}

data "mso_site" "site_2" {
name = "example_site_2"
}

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
sites = [data.mso_site.site_1.id, data.mso_site.site_2.id]
}

# tenant policies ipsla monitoring policy example

resource "mso_tenant_policies_ipsla_monitoring_policy" "ipsla_policy" {
template_id = mso_template.tenant_template.id
name = "ipsla_policy"
description = "Example description"
sla_type = "http"
http_version = "HTTP11"
http_uri = "/example1"
sla_frequency = 120
detect_multiplier = 4
request_data_size = 64
type_of_service = 18
operation_timeout = 100
threshold = 100
ipv6_traffic_class = 255
}
100 changes: 100 additions & 0 deletions mso/datasource_mso_tenant_policies_ipsla_monitoring_policy.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package mso

import (
"fmt"
"log"

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

func datasourceMSOIPSLAMonitoringPolicy() *schema.Resource {
return &schema.Resource{
Read: dataSourceMSOIPSLAMonitoringPolicyRead,

Schema: map[string]*schema.Schema{
"template_id": {
Type: schema.TypeString,
Required: true,
},
"name": {
Type: schema.TypeString,
Required: true,
},
"uuid": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"sla_type": {
Type: schema.TypeString,
Computed: true,
},
"destination_port": {
Type: schema.TypeInt,
Computed: true,
},
"http_version": {
Type: schema.TypeString,
Computed: true,
},
"http_uri": {
Type: schema.TypeString,
Computed: true,
},
"sla_frequency": {
Type: schema.TypeInt,
Computed: true,
},
"detect_multiplier": {
Type: schema.TypeInt,
Computed: true,
},
"request_data_size": {
Type: schema.TypeInt,
Computed: true,
},
"type_of_service": {
Type: schema.TypeInt,
Computed: true,
},
"operation_timeout": {
Type: schema.TypeInt,
Computed: true,
},
"threshold": {
Type: schema.TypeInt,
Computed: true,
},
"ipv6_traffic_class": {
Type: schema.TypeInt,
Computed: true,
},
},
}
}

func dataSourceMSOIPSLAMonitoringPolicyRead(d *schema.ResourceData, m interface{}) error {
log.Printf("[DEBUG] MSO IPSLA Monitoring 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", "ipslaMonitoringPolicies")
if err != nil {
return err
}

setIPSLAMonitoringPolicyData(d, policy, templateId)
log.Printf("[DEBUG] MSO IPSLA Monitoring Policy Data Source - Read Complete : %v", d.Id())
return nil
}
44 changes: 44 additions & 0 deletions mso/datasource_mso_tenant_policies_ipsla_monitoring_policy_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package mso

import (
"fmt"
"testing"

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

func TestAccMSOTenantPoliciesIPSLAMonitoringPolicyDataSource(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
Steps: []resource.TestStep{
{
PreConfig: func() { fmt.Println("Test: IPSLA Monitoring Policy Data Source") },
Config: testAccMSOTenantPoliciesIPSLAMonitoringPolicyDataSource(),
Check: resource.ComposeTestCheckFunc(
resource.TestCheckResourceAttr("data.mso_tenant_policies_ipsla_monitoring_policy.ipsla_policy", "name", "test_ipsla_policy"),
resource.TestCheckResourceAttr("data.mso_tenant_policies_ipsla_monitoring_policy.ipsla_policy", "description", "HTTP Type"),
resource.TestCheckResourceAttr("data.mso_tenant_policies_ipsla_monitoring_policy.ipsla_policy", "sla_type", "http"),
resource.TestCheckResourceAttr("data.mso_tenant_policies_ipsla_monitoring_policy.ipsla_policy", "destination_port", "80"),
resource.TestCheckResourceAttr("data.mso_tenant_policies_ipsla_monitoring_policy.ipsla_policy", "http_version", "HTTP11"),
resource.TestCheckResourceAttr("data.mso_tenant_policies_ipsla_monitoring_policy.ipsla_policy", "http_uri", "/example"),
resource.TestCheckResourceAttr("data.mso_tenant_policies_ipsla_monitoring_policy.ipsla_policy", "sla_frequency", "120"),
resource.TestCheckResourceAttr("data.mso_tenant_policies_ipsla_monitoring_policy.ipsla_policy", "detect_multiplier", "4"),
resource.TestCheckResourceAttr("data.mso_tenant_policies_ipsla_monitoring_policy.ipsla_policy", "request_data_size", "64"),
resource.TestCheckResourceAttr("data.mso_tenant_policies_ipsla_monitoring_policy.ipsla_policy", "type_of_service", "18"),
resource.TestCheckResourceAttr("data.mso_tenant_policies_ipsla_monitoring_policy.ipsla_policy", "operation_timeout", "100"),
resource.TestCheckResourceAttr("data.mso_tenant_policies_ipsla_monitoring_policy.ipsla_policy", "threshold", "100"),
resource.TestCheckResourceAttr("data.mso_tenant_policies_ipsla_monitoring_policy.ipsla_policy", "ipv6_traffic_class", "255"),
),
},
},
})
}

func testAccMSOTenantPoliciesIPSLAMonitoringPolicyDataSource() string {
return fmt.Sprintf(`%s
data "mso_tenant_policies_ipsla_monitoring_policy" "ipsla_policy" {
template_id = mso_tenant_policies_ipsla_monitoring_policy.ipsla_policy.template_id
name = "test_ipsla_policy"
}`, testAccMSOTenantPoliciesIPSLAMonitoringPolicyConfigCreate())
}
2 changes: 2 additions & 0 deletions mso/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ func Provider() terraform.ResourceProvider {
"mso_schema_site_contract_service_graph": resourceMSOSchemaSiteContractServiceGraph(),
"mso_schema_site_contract_service_graph_listener": resourceMSOSchemaSiteContractServiceGraphListener(),
"mso_template": resourceMSOTemplate(),
"mso_tenant_policies_ipsla_monitoring_policy": resourceMSOIPSLAMonitoringPolicy(),
},

DataSourcesMap: map[string]*schema.Resource{
Expand Down Expand Up @@ -174,6 +175,7 @@ func Provider() terraform.ResourceProvider {
"mso_schema_site_contract_service_graph": dataSourceMSOSchemaSiteContractServiceGraph(),
"mso_schema_site_contract_service_graph_listener": dataSourceMSOSchemaSiteContractServiceGraphListener(),
"mso_template": datasourceMSOTemplate(),
"mso_tenant_policies_ipsla_monitoring_policy": datasourceMSOIPSLAMonitoringPolicy(),
},

ConfigureFunc: configureClient,
Expand Down
62 changes: 51 additions & 11 deletions mso/provider_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package mso

import (
"fmt"
"os"
"sync"
"testing"

"github.com/ciscoecosystem/mso-go-client/client"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)
Expand All @@ -28,19 +31,56 @@ func TestProvider_impl(t *testing.T) {
var _ terraform.ResourceProvider = Provider()
}

func testAccPreCheck(t *testing.T) {
// We will use this function later on to make sure our test environment is valid.
// For example, you can make sure here that some environment variables are set.
if v := os.Getenv("MSO_USERNAME"); v == "" {
t.Fatal("username variable must be set for acceptance tests")
}
var (
msoClientTest *client.Client
msoClientTestOnce sync.Once
)

if v := os.Getenv("MSO_PASSWORD"); v == "" {
func testAccPreCheck(t *testing.T) *client.Client {
msoClientTestOnce.Do(func() {
var mso_url, mso_username, mso_password string
if v := os.Getenv("MSO_USERNAME"); v == "" {
t.Fatal("MSO_USERNAME must be set for acceptance tests")
} else {
mso_username = v
}
if v := os.Getenv("MSO_PASSWORD"); v == "" {
t.Fatal("MSO_PASSWORD must be set for acceptance tests")
} else {
mso_password = v
}
if v := os.Getenv("MSO_URL"); v == "" {
t.Fatal("MSO_URL must be set for acceptance tests")
} else {
mso_url = v
}

t.Fatal("password variable must be set for acceptance tests")
}
if v := os.Getenv("MSO_URL"); v == "" {
t.Fatal("url variable must be set for acceptance tests")
msoClientTest = client.GetClient(mso_url, mso_username, client.Password(mso_password), client.Insecure(true))
})
return msoClientTest

}

func testCheckResourceDestroyPolicyWithArguments(resource, policyType string) func(s *terraform.State) error {
return func(s *terraform.State) error {
return testCheckResourceDestroyPolicy(s, resource, policyType)
}
}

func testCheckResourceDestroyPolicy(s *terraform.State, resource, policyType string) error {
msoClient := testAccPreCheck(nil)
for name, rs := range s.RootModule().Resources {
if rs.Type == resource {
response, err := msoClient.GetViaURL((fmt.Sprintf("api/v1/templates/objects?type=%s&uuid=%s", policyType, rs.Primary.Attributes["uuid"])))
if err != nil {
if response.S("code").Data().(float64) == 404 {
continue
} else {
return fmt.Errorf("error checking if resource '%s' with ID '%s' still exists: %s", name, rs.Primary.ID, err)
}
}
return fmt.Errorf("terraform destroy was unsuccessful. The resource '%s' with ID '%s' still exists", name, rs.Primary.ID)
}
}
return nil
}
2 changes: 1 addition & 1 deletion mso/resource_mso_schema_site.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func resourceMSOSchemaSiteImport(d *schema.ResourceData, m interface{}) ([]*sche
if len(get_attribute) == 7 {
boolValue, err := strconv.ParseBool(get_attribute[6])
if err != nil {
return nil, fmt.Errorf("Boolean value for 'undeploy_on_destroy' cannot be parsed: %s", boolValue)
return nil, fmt.Errorf("Boolean value for 'undeploy_on_destroy' cannot be parsed: %v", boolValue)
}
d.Set("undeploy_on_destroy", boolValue)
} else {
Expand Down
Loading

0 comments on commit ae7c8b7

Please sign in to comment.