Skip to content

Commit

Permalink
[bugfix] ensure correct path is used for mso_schema_template_deploy_n…
Browse files Browse the repository at this point in the history
…do when provider platform is set to nd
  • Loading branch information
akinross authored and lhercot committed Feb 2, 2023
1 parent d7a97d2 commit af04c0f
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 8 deletions.
2 changes: 2 additions & 0 deletions examples/schema_template_deploy_ndo/main.tf
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@ terraform {
}
}

// mso_schema_template_deploy_ndo resource will only work when platform is set to nd
provider "mso" {
username = "" # <MSO username>
password = "" # <MSO pwd>
url = "" # <MSO URL>
insecure = true
platform = "nd"
}

resource "mso_site" "site_test_1" {
Expand Down
10 changes: 6 additions & 4 deletions mso/resource_mso_schema_site.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,21 +213,23 @@ func resourceMSOSchemaSiteDelete(d *schema.ResourceData, m interface{}) error {
log.Printf("[DEBUG] Parse of JSON failed with err: %s.", err)
return err
}
req, err := msoClient.MakeRestRequest("POST", "mso/api/v1/task", payload, true)
req, err := msoClient.MakeRestRequest("POST", "api/v1/task", payload, true)
if err != nil {
log.Printf("[DEBUG] MakeRestRequest failed with err: %s.", err)
return err
}
_, _, err = msoClient.Do(req)
if err != nil {
log.Printf("[DEBUG] Request failed with err: %s.", err)
_, resp, err := msoClient.Do(req)
if err != nil || resp.StatusCode != 202 {
log.Printf("[DEBUG] Request failed with resp: %v. Err: %s.", resp, err)
return err
}
} else if d.Get("undeploy_on_destroy").(bool) && err == nil {
_, err := msoClient.GetViaURL(fmt.Sprintf("/api/v1/execute/schema/%s/template/%s?undeploy=%s", schemaId, templateName, siteId))
if err != nil {
return err
}
} else if d.Get("undeploy_on_destroy").(bool) {
log.Printf("[WARNING] Failed to compare version. Template could not be undeployed prior to schema site deletion. Err: %s.", err)
}

schemasite := models.NewSchemaSite("remove", fmt.Sprintf("/sites/%s-%s", siteId, templateName), siteId, templateName)
Expand Down
18 changes: 14 additions & 4 deletions mso/resource_ndo_schema_template_deploy.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package mso

import (
"errors"
"fmt"
"log"

Expand All @@ -20,6 +21,15 @@ func resourceNDOSchemaTemplateDeploy() *schema.Resource {

SchemaVersion: version,

CustomizeDiff: func(diff *schema.ResourceDiff, v interface{}) error {
// Plan time validation.
msoClient := v.(*client.Client)
if msoClient.GetPlatform() != "nd" {
return errors.New(`The 'mso_schema_template_deploy_ndo' resource is only supported for nd based platforms, 'platform=nd' must be configured in the provider section of your configuration.`)
}
return nil
},

Schema: (map[string]*schema.Schema{
"schema_id": &schema.Schema{
Type: schema.TypeString,
Expand Down Expand Up @@ -52,7 +62,7 @@ func resourceNDOSchemaTemplateDeployExecute(d *schema.ResourceData, m interface{
log.Printf("[DEBUG] %s: Beginning Template Deploy Execution", d.Id())
templateName := d.Get("template_name").(string)
schemaId := d.Get("schema_id").(string)
path := "mso/api/v1/task"
path := "api/v1/task"

msoClient := m.(*client.Client)

Expand All @@ -71,9 +81,9 @@ func resourceNDOSchemaTemplateDeployExecute(d *schema.ResourceData, m interface{
log.Printf("[DEBUG] MakeRestRequest failed with err: %s.", err)
return err
}
_, _, err = msoClient.Do(req)
if err != nil {
log.Printf("[DEBUG] Request failed with err: %s.", err)
_, resp, err := msoClient.Do(req)
if err != nil || resp.StatusCode != 202 {
log.Printf("[DEBUG] Request failed with resp: %v. Err: %s.", resp, err)
return err
}

Expand Down
9 changes: 9 additions & 0 deletions website/docs/r/schema_template_deploy_ndo.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ Manages deploy and redeploy operations of schema templates for NDO v3.7 and high

```hcl
provider "mso" {
username = "" # <MSO username>
password = "" # <MSO pwd>
url = "" # <MSO URL>
insecure = true
platform = "nd"
}
resource "mso_schema_template_deploy_ndo" "template_deployer" {
schema_id = mso_schema.schema1.id
template_name = "Template1"
Expand All @@ -29,6 +37,7 @@ resource "mso_schema_template_deploy_ndo" "template_deployer" {

### Notes ###

* This resource requires 'platform = "nd"' to be configured in the provider configuration section.
* This resource is intentionally created non-idempotent so that it deploys the template in every run, it will not fail if there is no change and we deploy or redeploy the template again. When destroying the resource, no action is taken.
* Prior to deploy or redeploy a schema validation is executed. When schema validation fails, the resource will fail and deploy or redeploy will not be executed.
* A template can only be undeployed from a site by disassociating the site from the template with the resource mso_schema_site.
Expand Down

0 comments on commit af04c0f

Please sign in to comment.