Skip to content

Commit 59e7226

Browse files
authored
[minor_change] Add mso_schema_template_deploy_ndo resource to support NDO4.1+ deploy API (#165)
1 parent e10fa70 commit 59e7226

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+2076
-163
lines changed

examples/schema_site/main.tf

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,23 @@ provider "mso" {
1414
}
1515

1616
resource "mso_site" "site_1" {
17-
name = var.site_name_1
18-
username = "" # <site username>
19-
password = "" # <site pwd>
20-
apic_site_id = "105"
21-
urls = ["https://10.10.100.111"] # <site url>
17+
name = var.site_name_1
18+
username = "" # <site username>
19+
password = "" # <site pwd>
20+
apic_site_id = "105"
21+
urls = ["https://10.10.100.111"] # <site url>
2222
location = {
2323
lat = 78.946
2424
long = 95.623
2525
}
2626
}
2727

2828
resource "mso_site" "site_2" {
29-
name = var.site_name_2
30-
username = "" # <site username>
31-
password = "" # <site pwd>
32-
apic_site_id = "106"
33-
urls = ["https://10.10.10.125"] # <site url>
29+
name = var.site_name_2
30+
username = "" # <site username>
31+
password = "" # <site pwd>
32+
apic_site_id = "106"
33+
urls = ["https://10.10.10.125"] # <site url>
3434
location = {
3535
lat = 79.946
3636
long = 96.623
@@ -42,10 +42,10 @@ resource "mso_tenant" "tenant_1" {
4242
display_name = var.tenant_name
4343
description = "DemoTenant"
4444
site_associations {
45-
site_id = mso_site.site_1.id
45+
site_id = mso_site.site_1.id
4646
}
4747
site_associations {
48-
site_id = mso_site.site_2.id
48+
site_id = mso_site.site_2.id
4949
}
5050
}
5151

@@ -56,13 +56,15 @@ resource "mso_schema" "schema_1" {
5656
}
5757

5858
resource "mso_schema_site" "schema_site_1" {
59-
schema_id = mso_schema.schema_1.id
60-
site_id = mso_site.ansible_test.id
61-
template_name = var.template_name
59+
schema_id = mso_schema.schema_1.id
60+
site_id = mso_site.ansible_test.id
61+
template_name = var.template_name
6262
}
6363

64+
// when a template should be undeployed from site before disassociation the 'undeploy_on_delete' argument should be set to true prior to terraform destroy
6465
resource "mso_schema_site" "schema_site_2" {
65-
schema_id = mso_schema.schema_1.id
66-
site_id = mso_site.site_test_1.id
67-
template_name = var.template_name
66+
schema_id = mso_schema.schema_1.id
67+
site_id = mso_site.site_test_1.id
68+
template_name = var.template_name
69+
undeploy_on_delete = true
6870
}
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
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+
resource "mso_site" "site_test_1" {
17+
name = "site1"
18+
username = "admin"
19+
password = "test"
20+
apic_site_id = "100"
21+
urls = ["https://3.208.123.222"]
22+
}
23+
24+
resource "mso_tenant" "tenant1" {
25+
name = "test_tenant"
26+
display_name = "test_tenant"
27+
site_associations {
28+
site_id = mso_site.site_test_1.id
29+
}
30+
}
31+
32+
resource "mso_schema" "schema1" {
33+
name = "test_schema"
34+
template {
35+
name = "test_template"
36+
display_name = "test_template"
37+
tenant_id = mso_tenant.tenant1.id
38+
}
39+
}
40+
41+
resource "mso_schema_template_anp" "anp1" {
42+
schema_id = mso_schema.schema1.id
43+
template = tolist(mso_schema.schema1.template)[0].name
44+
name = "anp1"
45+
display_name = "anp1"
46+
}
47+
48+
// when a template should be undeployed from a site before disassociation the 'undeploy_on_destroy' argument should be set to true prior to terraform destroy
49+
resource "mso_schema_site" "schema_site1" {
50+
schema_id = mso_schema.schema1.id
51+
template_name = tolist(mso_schema.schema1.template)[0].name
52+
site_id = mso_site.site_test_1.id
53+
undeploy_on_destroy = true
54+
}
55+
56+
resource "mso_schema_template_deploy_ndo" "deploy_ndo" {
57+
schema_id = mso_schema.schema1.id
58+
template_name = tolist(mso_schema.schema1.template)[0].name
59+
}
60+
61+
// when a redeploy is preferred the boolean flag of re_deploy should be set to true as shown below
62+
resource "mso_schema_template_deploy_ndo" "redeploy_ndo" {
63+
schema_id = mso_schema.schema1.id
64+
template_name = tolist(mso_schema.schema1.template)[0].name
65+
re_deploy = true
66+
}

go.mod

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ module github.com/terraform-providers/terraform-provider-mso
33
go 1.13
44

55
require (
6-
github.com/ciscoecosystem/mso-go-client v1.8.0
6+
github.com/ciscoecosystem/mso-go-client v1.9.1
7+
github.com/hashicorp/go-version v1.6.0 // indirect
78
github.com/hashicorp/terraform-plugin-sdk v1.17.1
89
github.com/zclconf/go-cty v1.8.1 // indirect
910
)

go.sum

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ github.com/cheggaaa/pb v1.0.27/go.mod h1:pQciLPpbU0oxA0h+VJYYLxO+XeDQb5pZijXscXH
7878
github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI=
7979
github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI=
8080
github.com/chzyer/test v0.0.0-20180213035817-a1ea475d72b1/go.mod h1:Q3SI9o4m/ZMnBNeIyt5eFwwo7qiLfzFZmjNmxjkiQlU=
81-
github.com/ciscoecosystem/mso-go-client v1.8.0 h1:xCt+WWImiru23idjo63kT5FCHyQH5HvOXnHmYVMj3xA=
82-
github.com/ciscoecosystem/mso-go-client v1.8.0/go.mod h1:QEU4sm4aLL6RPIjCF6Dw4VwLKZZ25J8GcPLjD1+m/u4=
81+
github.com/ciscoecosystem/mso-go-client v1.9.1 h1:0s1ubQpNqraiHjGOUxAWpr/GQrKM4WnZXdzroGg+SZM=
82+
github.com/ciscoecosystem/mso-go-client v1.9.1/go.mod h1:swAjH8WZGE4eeKpsNE4DtNzOdREFXI8SkerdbrXwZac=
8383
github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw=
8484
github.com/cncf/udpa/go v0.0.0-20191209042840-269d4d468f6f/go.mod h1:M8M6+tZqaGXZJjfX53e64911xZQV5JYwmTeXPW+k8Sc=
8585
github.com/creack/pty v1.1.9/go.mod h1:oKZEueFk5CKHvIhNR5MUki03XCEU+Q6VDXinZuGJ33E=
@@ -195,6 +195,8 @@ github.com/hashicorp/go-version v1.1.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09
195195
github.com/hashicorp/go-version v1.2.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
196196
github.com/hashicorp/go-version v1.2.1 h1:zEfKbn2+PDgroKdiOzqiE8rsmLqU2uwi5PB5pBJ3TkI=
197197
github.com/hashicorp/go-version v1.2.1/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
198+
github.com/hashicorp/go-version v1.6.0 h1:feTTfFNnjP967rlCxM/I9g701jU+RN74YKx2mOkIeek=
199+
github.com/hashicorp/go-version v1.6.0/go.mod h1:fltr4n8CU8Ke44wwGCBoEymUuxUHl09ZGVZPK5anwXA=
198200
github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
199201
github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8=
200202
github.com/hashicorp/hcl v0.0.0-20170504190234-a4b07c25de5f h1:UdxlrJz4JOnY8W+DbLISwf2B8WXEolNRA8BGCwI9jws=

mso/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ func Provider() terraform.ResourceProvider {
9292
"mso_schema_site_bd_subnet": resourceMSOSchemaSiteBdSubnet(),
9393
"mso_rest": resourceMSORest(),
9494
"mso_schema_template_deploy": resourceMSOSchemaTemplateDeploy(),
95+
"mso_schema_template_deploy_ndo": resourceNDOSchemaTemplateDeploy(),
9596
"mso_schema_site_vrf_region_cidr_subnet": resourceMSOSchemaSiteVrfRegionCidrSubnet(),
9697
"mso_schema_site_vrf_region_cidr": resourceMSOSchemaSiteVrfRegionCidr(),
9798
"mso_schema_site_anp": resourceMSOSchemaSiteAnp(),

mso/resource_mso_schema_site.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77

88
"github.com/ciscoecosystem/mso-go-client/client"
9+
"github.com/ciscoecosystem/mso-go-client/container"
910
"github.com/ciscoecosystem/mso-go-client/models"
1011

1112
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
@@ -16,6 +17,7 @@ func resourceMSOSchemaSite() *schema.Resource {
1617
return &schema.Resource{
1718
Create: resourceMSOSchemaSiteCreate,
1819
Read: resourceMSOSchemaSiteRead,
20+
Update: resourceMSOSchemaSiteUpdate,
1921
Delete: resourceMSOSchemaSiteDelete,
2022

2123
Importer: &schema.ResourceImporter{
@@ -45,6 +47,12 @@ func resourceMSOSchemaSite() *schema.Resource {
4547
ForceNew: true,
4648
ValidateFunc: validation.StringLenBetween(1, 1000),
4749
},
50+
51+
"undeploy_on_destroy": &schema.Schema{
52+
Type: schema.TypeBool,
53+
Default: false,
54+
Optional: true,
55+
},
4856
}),
4957
}
5058
}
@@ -185,13 +193,43 @@ func resourceMSOSchemaSiteRead(d *schema.ResourceData, m interface{}) error {
185193
return nil
186194
}
187195

196+
func resourceMSOSchemaSiteUpdate(d *schema.ResourceData, m interface{}) error {
197+
d.Set("undeploy_on_destroy", d.Get("undeploy_on_destroy").(bool))
198+
return nil
199+
}
200+
188201
func resourceMSOSchemaSiteDelete(d *schema.ResourceData, m interface{}) error {
189202
log.Printf("[DEBUG] %s: Beginning Destroy", d.Id())
190203
msoClient := m.(*client.Client)
191204
schemaId := d.Get("schema_id").(string)
192205
siteId := d.Get("site_id").(string)
193206
templateName := d.Get("template_name").(string)
194207

208+
versionInt, err := msoClient.CompareVersion("3.7.0.0")
209+
210+
if d.Get("undeploy_on_destroy").(bool) && versionInt == -1 {
211+
payload, err := container.ParseJSON([]byte(fmt.Sprintf(`{"schemaId": "%s", "templateName": "%s", "undeploy": ["%s"]}`, schemaId, templateName, siteId)))
212+
if err != nil {
213+
log.Printf("[DEBUG] Parse of JSON failed with err: %s.", err)
214+
return err
215+
}
216+
req, err := msoClient.MakeRestRequest("POST", "mso/api/v1/task", payload, true)
217+
if err != nil {
218+
log.Printf("[DEBUG] MakeRestRequest failed with err: %s.", err)
219+
return err
220+
}
221+
_, _, err = msoClient.Do(req)
222+
if err != nil {
223+
log.Printf("[DEBUG] Request failed with err: %s.", err)
224+
return err
225+
}
226+
} else if d.Get("undeploy_on_destroy").(bool) && err == nil {
227+
_, err := msoClient.GetViaURL(fmt.Sprintf("/api/v1/execute/schema/%s/template/%s?undeploy=%s", schemaId, templateName, siteId))
228+
if err != nil {
229+
return err
230+
}
231+
}
232+
195233
schemasite := models.NewSchemaSite("remove", fmt.Sprintf("/sites/%s-%s", siteId, templateName), siteId, templateName)
196234

197235
response, err := msoClient.PatchbyID(fmt.Sprintf("api/v1/schemas/%s", schemaId), schemasite)
Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
package mso
2+
3+
import (
4+
"fmt"
5+
"log"
6+
7+
"github.com/ciscoecosystem/mso-go-client/client"
8+
"github.com/ciscoecosystem/mso-go-client/container"
9+
"github.com/ciscoecosystem/mso-go-client/models"
10+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
11+
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
12+
)
13+
14+
func resourceNDOSchemaTemplateDeploy() *schema.Resource {
15+
return &schema.Resource{
16+
Create: resourceNDOSchemaTemplateDeployExecute,
17+
Read: resourceNDOSchemaTemplateDeployRead,
18+
Update: resourceNDOSchemaTemplateDeployExecute,
19+
Delete: resourceNDOSchemaTemplateDeployDelete,
20+
21+
SchemaVersion: version,
22+
23+
Schema: (map[string]*schema.Schema{
24+
"schema_id": &schema.Schema{
25+
Type: schema.TypeString,
26+
Required: true,
27+
ValidateFunc: validation.StringLenBetween(1, 1000),
28+
},
29+
30+
"template_name": &schema.Schema{
31+
Type: schema.TypeString,
32+
Required: true,
33+
ValidateFunc: validation.StringLenBetween(1, 1000),
34+
},
35+
36+
"re_deploy": &schema.Schema{
37+
Type: schema.TypeBool,
38+
Optional: true,
39+
Default: false,
40+
},
41+
42+
"force_apply": &schema.Schema{
43+
Type: schema.TypeString,
44+
Optional: true,
45+
Default: "always-deploy",
46+
},
47+
}),
48+
}
49+
}
50+
51+
func resourceNDOSchemaTemplateDeployExecute(d *schema.ResourceData, m interface{}) error {
52+
log.Printf("[DEBUG] %s: Beginning Template Deploy Execution", d.Id())
53+
templateName := d.Get("template_name").(string)
54+
schemaId := d.Get("schema_id").(string)
55+
path := "mso/api/v1/task"
56+
57+
msoClient := m.(*client.Client)
58+
59+
schemaValidate := models.SchemValidate{SchmaId: d.Get("schema_id").(string)}
60+
_, err := msoClient.ReadSchemaValidate(&schemaValidate)
61+
if err != nil {
62+
return err
63+
}
64+
payload, err := container.ParseJSON([]byte(fmt.Sprintf(`{"schemaId": "%s", "templateName": "%s", "isRedeploy": %v}`, schemaId, templateName, d.Get("re_deploy").(bool))))
65+
if err != nil {
66+
log.Printf("[DEBUG] Parse of JSON failed with err: %s.", err)
67+
return err
68+
}
69+
req, err := msoClient.MakeRestRequest("POST", path, payload, true)
70+
if err != nil {
71+
log.Printf("[DEBUG] MakeRestRequest failed with err: %s.", err)
72+
return err
73+
}
74+
_, _, err = msoClient.Do(req)
75+
if err != nil {
76+
log.Printf("[DEBUG] Request failed with err: %s.", err)
77+
return err
78+
}
79+
80+
d.SetId(schemaId)
81+
log.Printf("[DEBUG] %s: Successful Template Deploy Execution", d.Id())
82+
return resourceNDOSchemaTemplateDeployRead(d, m)
83+
}
84+
85+
func resourceNDOSchemaTemplateDeployRead(d *schema.ResourceData, m interface{}) error {
86+
d.Set("force_apply", "")
87+
return nil
88+
}
89+
90+
func resourceNDOSchemaTemplateDeployDelete(d *schema.ResourceData, m interface{}) error {
91+
return nil
92+
}

0 commit comments

Comments
 (0)