-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[minor_change] Add resource and datasource for mso_template
- Loading branch information
Showing
8 changed files
with
1,705 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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_id = data.mso_tenant.example_tenant.id | ||
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_id = data.mso_tenant.example_tenant.id | ||
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_id = data.mso_tenant.example_tenant.id | ||
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_id = data.mso_tenant.example_tenant.id | ||
sites = [data.mso_site.site_1.id, data.mso_site.site_2.id] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
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, | ||
ValidateFunc: validation.StringLenBetween(1, 1000), | ||
}, | ||
"template_name": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ValidateFunc: validation.StringLenBetween(1, 1000), | ||
}, | ||
"template_type": &schema.Schema{ | ||
Type: schema.TypeString, | ||
Optional: true, | ||
ValidateFunc: validation.StringInSlice([]string{ | ||
"tenant", | ||
"l3out", | ||
"fabric_policy", | ||
"fabric_resource", | ||
"monitoring_tenant", | ||
"monitoring_access", | ||
"service_device", | ||
}, false), | ||
}, | ||
"tenant_id": &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 | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,143 @@ | ||
package mso | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-sdk/helper/resource" | ||
) | ||
|
||
func TestAccMSOTemplateDatasourceTenantErrors(t *testing.T) { | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
PreConfig: func() { fmt.Println("Test: No template_id or template_name provided in Template configuration") }, | ||
Config: testAccMSOTemplateDatasourceErrorNoIdOrNameConfig(), | ||
ExpectError: regexp.MustCompile("either `template_id` or `template_name` must be provided"), | ||
}, | ||
{ | ||
PreConfig: func() { fmt.Println("Test: No template_type with name provided in Template configuration") }, | ||
Config: testAccMSOTemplateDatasourceErrorNoTypeConfig(), | ||
ExpectError: regexp.MustCompile("`template_type` must be provided when `template_name` is provided"), | ||
}, | ||
{ | ||
PreConfig: func() { fmt.Println("Test: Both template_id and template_name provided in Template configuration") }, | ||
Config: testAccMSOTemplateDatasourceErrorIdAndNameConfig(), | ||
ExpectError: regexp.MustCompile("only one of `template_id` or `template_name` must be provided"), | ||
}, | ||
{ | ||
PreConfig: func() { fmt.Println("Test: Non existing template name provided in Template configuration") }, | ||
Config: testAccMSOTemplateDatasourceErrorNonExistingConfig(), | ||
ExpectError: regexp.MustCompile("Template with name 'non_existing_template_name' not found."), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccMSOTemplateDatasourceTenantName(t *testing.T) { | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
PreConfig: func() { fmt.Println("Test: Tenant template with name and type provided in Template configuration") }, | ||
Config: testAccMSOTemplateDatasourceNameAndTypeConfig(), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccMSOTemplateState( | ||
"data.mso_template.template_tenant", | ||
&TemplateTest{ | ||
TemplateName: "test_template_tenant", | ||
TemplateType: "tenant", | ||
Tenant: msoTemplateTenantName, | ||
Sites: []string{msoTemplateSiteName1, msoTemplateSiteName2}, | ||
}, | ||
false, | ||
), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func TestAccMSOTemplateDatasourceTenantId(t *testing.T) { | ||
|
||
resource.Test(t, resource.TestCase{ | ||
PreCheck: func() { testAccPreCheck(t) }, | ||
Providers: testAccProviders, | ||
Steps: []resource.TestStep{ | ||
{ | ||
PreConfig: func() { fmt.Println("Test: Tenant template with id provided in Template configuration") }, | ||
Config: testAccMSOTemplateDatasourceIdConfig(), | ||
Check: resource.ComposeTestCheckFunc( | ||
testAccMSOTemplateState( | ||
"data.mso_template.template_tenant", | ||
&TemplateTest{ | ||
TemplateName: "test_template_tenant", | ||
TemplateType: "tenant", | ||
Tenant: msoTemplateTenantName, | ||
Sites: []string{msoTemplateSiteName1, msoTemplateSiteName2}, | ||
}, | ||
false, | ||
), | ||
), | ||
}, | ||
}, | ||
}) | ||
} | ||
|
||
func testAccMSOTemplateDatasourceNameAndTypeConfig() string { | ||
return fmt.Sprintf(`%s | ||
data "mso_template" "template_tenant" { | ||
template_name = mso_template.template_tenant.template_name | ||
template_type = "tenant" | ||
} | ||
`, testAccMSOTemplateResourceTenanTwoSitesConfig()) | ||
} | ||
|
||
func testAccMSOTemplateDatasourceIdConfig() string { | ||
return fmt.Sprintf(`%s | ||
data "mso_template" "template_tenant" { | ||
template_id = mso_template.template_tenant.id | ||
} | ||
`, testAccMSOTemplateResourceTenanTwoSitesConfig()) | ||
} | ||
|
||
func testAccMSOTemplateDatasourceErrorNoIdOrNameConfig() string { | ||
return fmt.Sprintf(` | ||
data "mso_template" "template_tenant" { | ||
template_type = "tenant" | ||
} | ||
`) | ||
} | ||
|
||
func testAccMSOTemplateDatasourceErrorNoTypeConfig() string { | ||
return fmt.Sprintf(` | ||
data "mso_template" "template_tenant" { | ||
template_name = "non_existing_template_name" | ||
} | ||
`) | ||
} | ||
|
||
func testAccMSOTemplateDatasourceErrorIdAndNameConfig() string { | ||
return fmt.Sprintf(` | ||
data "mso_template" "template_tenant" { | ||
template_id = "non_existing_template_id" | ||
template_name = "non_existing_template_name" | ||
template_type = "tenant" | ||
} | ||
`) | ||
} | ||
|
||
func testAccMSOTemplateDatasourceErrorNonExistingConfig() string { | ||
return fmt.Sprintf(` | ||
data "mso_template" "template_tenant" { | ||
template_name = "non_existing_template_name" | ||
template_type = "tenant" | ||
} | ||
`) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.