|
| 1 | +/* Copyright © 2020 VMware, Inc. All Rights Reserved. |
| 2 | + SPDX-License-Identifier: MPL-2.0 */ |
| 3 | + |
| 4 | +package nsxt |
| 5 | + |
| 6 | +import ( |
| 7 | + "fmt" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | + "github.com/vmware/vsphere-automation-sdk-go/runtime/protocol/client" |
| 11 | + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/infra/sha" |
| 12 | + "github.com/vmware/vsphere-automation-sdk-go/services/nsxt/model" |
| 13 | +) |
| 14 | + |
| 15 | +func resourceNsxtPolicyODSRunbookInvocation() *schema.Resource { |
| 16 | + return &schema.Resource{ |
| 17 | + Create: resourceNsxtPolicyODSRunbookInvocationCreate, |
| 18 | + Read: resourceNsxtPolicyODSRunbookInvocationRead, |
| 19 | + Update: resourceNsxtPolicyODSRunbookInvocationUpdate, |
| 20 | + Delete: resourceNsxtPolicyODSRunbookInvocationDelete, |
| 21 | + Importer: &schema.ResourceImporter{ |
| 22 | + State: nsxtPolicyPathResourceImporter, |
| 23 | + }, |
| 24 | + |
| 25 | + Schema: map[string]*schema.Schema{ |
| 26 | + "nsx_id": getNsxIDSchema(), |
| 27 | + "path": getPathSchema(), |
| 28 | + "display_name": getDisplayNameSchema(), |
| 29 | + "description": getDescriptionSchema(), |
| 30 | + "revision": getRevisionSchema(), |
| 31 | + "tag": getTagsSchema(), |
| 32 | + "argument": { |
| 33 | + Type: schema.TypeSet, |
| 34 | + Optional: true, |
| 35 | + Description: "Arguments for runbook invocation", |
| 36 | + Elem: &schema.Resource{ |
| 37 | + Schema: map[string]*schema.Schema{ |
| 38 | + "key": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Required: true, |
| 41 | + Description: "Key", |
| 42 | + }, |
| 43 | + "value": { |
| 44 | + Type: schema.TypeString, |
| 45 | + Required: true, |
| 46 | + Description: "Value", |
| 47 | + }, |
| 48 | + }, |
| 49 | + }, |
| 50 | + }, |
| 51 | + "runbook_path": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Required: true, |
| 54 | + Description: "Path of runbook object", |
| 55 | + }, |
| 56 | + "target_node": { |
| 57 | + Type: schema.TypeString, |
| 58 | + Optional: true, |
| 59 | + Description: "Identifier of an appliance node or transport node", |
| 60 | + }, |
| 61 | + }, |
| 62 | + } |
| 63 | +} |
| 64 | + |
| 65 | +func getODSRunbookInvocationFromSchema(id string, d *schema.ResourceData) model.OdsRunbookInvocation { |
| 66 | + displayName := d.Get("display_name").(string) |
| 67 | + description := d.Get("description").(string) |
| 68 | + tags := getPolicyTagsFromSchema(d) |
| 69 | + runbookPath := d.Get("runbook_path").(string) |
| 70 | + targetNode := d.Get("target_node").(string) |
| 71 | + |
| 72 | + var arguments []model.UnboundedKeyValuePair |
| 73 | + for _, arg := range d.Get("argument").(*schema.Set).List() { |
| 74 | + argMap := arg.(map[string]interface{}) |
| 75 | + key := argMap["key"].(string) |
| 76 | + value := argMap["value"].(string) |
| 77 | + item := model.UnboundedKeyValuePair{ |
| 78 | + Key: &key, |
| 79 | + Value: &value, |
| 80 | + } |
| 81 | + arguments = append(arguments, item) |
| 82 | + } |
| 83 | + |
| 84 | + obj := model.OdsRunbookInvocation{ |
| 85 | + Id: &id, |
| 86 | + DisplayName: &displayName, |
| 87 | + Description: &description, |
| 88 | + Tags: tags, |
| 89 | + RunbookPath: &runbookPath, |
| 90 | + Arguments: arguments, |
| 91 | + TargetNode: &targetNode, |
| 92 | + } |
| 93 | + |
| 94 | + return obj |
| 95 | +} |
| 96 | + |
| 97 | +func resourceNsxtPolicyODSRunbookInvocationCreate(d *schema.ResourceData, m interface{}) error { |
| 98 | + // Initialize resource Id and verify this ID is not yet used |
| 99 | + id, err := getOrGenerateID(d, m, resourceNsxtPolicyODSRunbookInvocationExists) |
| 100 | + if err != nil { |
| 101 | + return err |
| 102 | + } |
| 103 | + |
| 104 | + connector := getPolicyConnector(m) |
| 105 | + client := sha.NewRunbookInvocationsClient(connector) |
| 106 | + |
| 107 | + obj := getODSRunbookInvocationFromSchema(id, d) |
| 108 | + err = client.Create(id, obj) |
| 109 | + if err != nil { |
| 110 | + return handleCreateError("OdsRunbookInvocation", id, err) |
| 111 | + } |
| 112 | + |
| 113 | + d.SetId(id) |
| 114 | + d.Set("nsx_id", id) |
| 115 | + return nil |
| 116 | +} |
| 117 | + |
| 118 | +func resourceNsxtPolicyODSRunbookInvocationExists(id string, connector client.Connector, isGlobalManager bool) (bool, error) { |
| 119 | + var err error |
| 120 | + client := sha.NewRunbookInvocationsClient(connector) |
| 121 | + _, err = client.Get(id) |
| 122 | + |
| 123 | + if err == nil { |
| 124 | + return true, nil |
| 125 | + } |
| 126 | + |
| 127 | + if isNotFoundError(err) { |
| 128 | + return false, nil |
| 129 | + } |
| 130 | + |
| 131 | + return false, logAPIError("Error retrieving resource", err) |
| 132 | +} |
| 133 | + |
| 134 | +func resourceNsxtPolicyODSRunbookInvocationRead(d *schema.ResourceData, m interface{}) error { |
| 135 | + connector := getPolicyConnector(m) |
| 136 | + |
| 137 | + id := d.Id() |
| 138 | + if id == "" { |
| 139 | + return fmt.Errorf("error obtaining OdsRunbookInvocation ID") |
| 140 | + } |
| 141 | + |
| 142 | + client := sha.NewRunbookInvocationsClient(connector) |
| 143 | + var err error |
| 144 | + obj, err := client.Get(id) |
| 145 | + if err != nil { |
| 146 | + return handleReadError(d, "OdsRunbookInvocation", id, err) |
| 147 | + } |
| 148 | + |
| 149 | + d.Set("display_name", obj.DisplayName) |
| 150 | + d.Set("description", obj.Description) |
| 151 | + setPolicyTagsInSchema(d, obj.Tags) |
| 152 | + d.Set("nsx_id", id) |
| 153 | + d.Set("path", obj.Path) |
| 154 | + d.Set("revision", obj.Revision) |
| 155 | + |
| 156 | + d.Set("runbook_path", obj.RunbookPath) |
| 157 | + d.Set("target_node", obj.TargetNode) |
| 158 | + |
| 159 | + var argList []map[string]interface{} |
| 160 | + for _, arg := range obj.Arguments { |
| 161 | + argData := make(map[string]interface{}) |
| 162 | + argData["key"] = arg.Key |
| 163 | + argData["value"] = arg.Value |
| 164 | + argList = append(argList, argData) |
| 165 | + } |
| 166 | + d.Set("argument", argList) |
| 167 | + |
| 168 | + return nil |
| 169 | +} |
| 170 | + |
| 171 | +func resourceNsxtPolicyODSRunbookInvocationUpdate(d *schema.ResourceData, m interface{}) error { |
| 172 | + return nil |
| 173 | +} |
| 174 | + |
| 175 | +func resourceNsxtPolicyODSRunbookInvocationDelete(d *schema.ResourceData, m interface{}) error { |
| 176 | + id := d.Id() |
| 177 | + if id == "" { |
| 178 | + return fmt.Errorf("error obtaining OdsRunbookInvocation ID") |
| 179 | + } |
| 180 | + |
| 181 | + connector := getPolicyConnector(m) |
| 182 | + var err error |
| 183 | + client := sha.NewRunbookInvocationsClient(connector) |
| 184 | + err = client.Delete(id) |
| 185 | + |
| 186 | + if err != nil { |
| 187 | + return handleDeleteError("OdsRunbookInvocation", id, err) |
| 188 | + } |
| 189 | + |
| 190 | + return nil |
| 191 | +} |
0 commit comments