|
| 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