Skip to content

Commit

Permalink
[minor_change] Add resource and datasource for mso_template
Browse files Browse the repository at this point in the history
  • Loading branch information
akinross committed Oct 23, 2024
1 parent 40585d8 commit 322cd71
Show file tree
Hide file tree
Showing 6 changed files with 837 additions and 0 deletions.
86 changes: 86 additions & 0 deletions examples/template/main.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
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_name = mso_tenant.example_tenant.display_name
sites = [data.mso_site.site_1.id, data.mso_site.site_2.id]
}

# l3out template example

resource "mso_template" "l3out_template" {
template_name = "l3out_template"
template_type = "l3out"
tenant_name = mso_tenant.example_tenant.display_name
sites = [data.mso_site.site_1.id]
}

# fabric policy template example

resource "mso_template" "fabric_policy_template" {
template_name = "fabric_policy_template"
template_type = "fabric_policy"
sites = [data.mso_site.site_1.id, data.mso_site.site_2.id]
}

# fabric resource template example

resource "mso_template" "fabric_resource_template" {
template_name = "fabric_resource_template"
template_type = "fabric_resource"
sites = [data.mso_site.site_1.id, data.mso_site.site_2.id]
}

# monitoring tenant template example

resource "mso_template" "monitoring_tenant_template" {
template_name = "monitoring_tenant_template"
template_type = "monitoring_tenant"
tenant_name = mso_tenant.example_tenant.display_name
sites = [data.mso_site.site_1.id]
}

# monitoring access template example

resource "mso_template" "monitoring_access_template" {
template_name = "monitoring_access_template"
template_type = "monitoring_access"
sites = [data.mso_site.site_1.id]
}

# service device template example

resource "mso_template" "service_device_template" {
template_name = "service_device_template"
template_type = "service_device"
tenant_name = mso_tenant.example_tenant.display_name
sites = [data.mso_site.site_1.id, data.mso_site.site_2.id]
}
87 changes: 87 additions & 0 deletions mso/datasource_mso_template.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package mso

import (
"fmt"
"log"

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

func datasourceMSOTemplate() *schema.Resource {
return &schema.Resource{

Read: datasourceMSOTemplateRead,

SchemaVersion: version,

Schema: (map[string]*schema.Schema{
"template_id": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringLenBetween(1, 1000),
},
"template_name": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringLenBetween(1, 1000),
},
"template_type": &schema.Schema{
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateFunc: validation.StringInSlice([]string{
"tenant",
"l3out",
"fabric_policy",
"fabric_resource",
"monitoring_tenant",
"monitoring_access",
"service_device",
}, false),
},
"tenant_name": &schema.Schema{
Type: schema.TypeString,
Computed: true,
},
"sites": &schema.Schema{
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
}),
}
}

func datasourceMSOTemplateRead(d *schema.ResourceData, m interface{}) error {
log.Println("[DEBUG] MSO Template Datasource: Beginning Read")

msoClient := m.(*client.Client)
id := d.Get("template_id").(string)
name := d.Get("template_name").(string)
templateType := d.Get("template_type").(string)

if id == "" && name == "" {
return fmt.Errorf("either `template_id` or `template_name` must be provided")
} else if id != "" && name != "" {
return fmt.Errorf("only one of `template_id` or `template_name` must be provided")
} else if name != "" && templateType == "" {
return fmt.Errorf("`template_type` must be provided when `template_name` is provided")
}

ndoTemplate := ndoTemplate{msoClient: msoClient, id: id, templateName: name, templateType: templateType}
err := ndoTemplate.getTemplate(true)
if err != nil {
return err
}
ndoTemplate.SetToSchema(d)
d.Set("template_id", d.Id())
log.Println("[DEBUG] MSO Template Datasource: Read Completed", d.Id())
return nil

}
2 changes: 2 additions & 0 deletions mso/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ func Provider() terraform.ResourceProvider {
"mso_system_config": resourceMSOSystemConfig(),
"mso_schema_site_contract_service_graph": resourceMSOSchemaSiteContractServiceGraph(),
"mso_schema_site_contract_service_graph_listener": resourceMSOSchemaSiteContractServiceGraphListener(),
"mso_template": resourceMSOTemplate(),
},

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

ConfigureFunc: configureClient,
Expand Down
Loading

0 comments on commit 322cd71

Please sign in to comment.