|
| 1 | +package aci |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "log" |
| 7 | + |
| 8 | + "github.com/ciscoecosystem/aci-go-client/client" |
| 9 | + "github.com/ciscoecosystem/aci-go-client/models" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 13 | +) |
| 14 | + |
| 15 | +func resourceAciEndpointSecurityGroupTagSelector() *schema.Resource { |
| 16 | + return &schema.Resource{ |
| 17 | + CreateContext: resourceAciEndpointSecurityGroupTagSelectorCreate, |
| 18 | + UpdateContext: resourceAciEndpointSecurityGroupTagSelectorUpdate, |
| 19 | + ReadContext: resourceAciEndpointSecurityGroupTagSelectorRead, |
| 20 | + DeleteContext: resourceAciEndpointSecurityGroupTagSelectorDelete, |
| 21 | + |
| 22 | + Importer: &schema.ResourceImporter{ |
| 23 | + State: resourceAciEndpointSecurityGroupTagSelectorImport, |
| 24 | + }, |
| 25 | + |
| 26 | + SchemaVersion: 1, |
| 27 | + Schema: AppendBaseAttrSchema(AppendNameAliasAttrSchema(map[string]*schema.Schema{ |
| 28 | + "endpoint_security_group_dn": { |
| 29 | + Type: schema.TypeString, |
| 30 | + Required: true, |
| 31 | + ForceNew: true, |
| 32 | + }, |
| 33 | + "match_key": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Required: true, |
| 36 | + ForceNew: true, |
| 37 | + }, |
| 38 | + "match_value": { |
| 39 | + Type: schema.TypeString, |
| 40 | + Required: true, |
| 41 | + ForceNew: true, |
| 42 | + }, |
| 43 | + "name": { |
| 44 | + Type: schema.TypeString, |
| 45 | + Optional: true, |
| 46 | + Computed: true, |
| 47 | + }, |
| 48 | + "value_operator": { |
| 49 | + Type: schema.TypeString, |
| 50 | + Optional: true, |
| 51 | + Computed: true, |
| 52 | + ValidateFunc: validation.StringInSlice([]string{ |
| 53 | + "contains", |
| 54 | + "equals", |
| 55 | + "regex", |
| 56 | + }, false), |
| 57 | + }, |
| 58 | + })), |
| 59 | + } |
| 60 | +} |
| 61 | + |
| 62 | +func getRemoteEndpointSecurityGroupTagSelector(client *client.Client, dn string) (*models.EndpointSecurityGroupTagSelector, error) { |
| 63 | + fvTagSelectorCont, err := client.Get(dn) |
| 64 | + if err != nil { |
| 65 | + return nil, err |
| 66 | + } |
| 67 | + fvTagSelector := models.EndpointSecurityGroupTagSelectorFromContainer(fvTagSelectorCont) |
| 68 | + if fvTagSelector.DistinguishedName == "" { |
| 69 | + return nil, fmt.Errorf("EndpointSecurityGroupTagSelector %s not found", fvTagSelector.DistinguishedName) |
| 70 | + } |
| 71 | + return fvTagSelector, nil |
| 72 | +} |
| 73 | + |
| 74 | +func setEndpointSecurityGroupTagSelectorAttributes(fvTagSelector *models.EndpointSecurityGroupTagSelector, d *schema.ResourceData) (*schema.ResourceData, error) { |
| 75 | + d.SetId(fvTagSelector.DistinguishedName) |
| 76 | + d.Set("description", fvTagSelector.Description) |
| 77 | + fvTagSelectorMap, err := fvTagSelector.ToMap() |
| 78 | + if err != nil { |
| 79 | + return d, err |
| 80 | + } |
| 81 | + d.Set("annotation", fvTagSelectorMap["annotation"]) |
| 82 | + d.Set("match_key", fvTagSelectorMap["matchKey"]) |
| 83 | + d.Set("match_value", fvTagSelectorMap["matchValue"]) |
| 84 | + d.Set("name", fvTagSelectorMap["name"]) |
| 85 | + d.Set("value_operator", fvTagSelectorMap["valueOperator"]) |
| 86 | + d.Set("name_alias", fvTagSelectorMap["nameAlias"]) |
| 87 | + return d, nil |
| 88 | +} |
| 89 | + |
| 90 | +func resourceAciEndpointSecurityGroupTagSelectorImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) { |
| 91 | + log.Printf("[DEBUG] %s: Beginning Import", d.Id()) |
| 92 | + aciClient := m.(*client.Client) |
| 93 | + dn := d.Id() |
| 94 | + fvTagSelector, err := getRemoteEndpointSecurityGroupTagSelector(aciClient, dn) |
| 95 | + if err != nil { |
| 96 | + return nil, err |
| 97 | + } |
| 98 | + schemaFilled, err := setEndpointSecurityGroupTagSelectorAttributes(fvTagSelector, d) |
| 99 | + if err != nil { |
| 100 | + return nil, err |
| 101 | + } |
| 102 | + log.Printf("[DEBUG] %s: Import finished successfully", d.Id()) |
| 103 | + return []*schema.ResourceData{schemaFilled}, nil |
| 104 | +} |
| 105 | + |
| 106 | +func resourceAciEndpointSecurityGroupTagSelectorCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 107 | + log.Printf("[DEBUG] EndpointSecurityGroupTagSelector: Beginning Creation") |
| 108 | + aciClient := m.(*client.Client) |
| 109 | + desc := d.Get("description").(string) |
| 110 | + matchKey := d.Get("match_key").(string) |
| 111 | + matchValue := d.Get("match_value").(string) |
| 112 | + EndpointSecurityGroupDn := d.Get("endpoint_security_group_dn").(string) |
| 113 | + |
| 114 | + fvTagSelectorAttr := models.EndpointSecurityGroupTagSelectorAttributes{} |
| 115 | + nameAlias := "" |
| 116 | + if NameAlias, ok := d.GetOk("name_alias"); ok { |
| 117 | + nameAlias = NameAlias.(string) |
| 118 | + } |
| 119 | + if Annotation, ok := d.GetOk("annotation"); ok { |
| 120 | + fvTagSelectorAttr.Annotation = Annotation.(string) |
| 121 | + } else { |
| 122 | + fvTagSelectorAttr.Annotation = "{}" |
| 123 | + } |
| 124 | + |
| 125 | + if MatchKey, ok := d.GetOk("match_key"); ok { |
| 126 | + fvTagSelectorAttr.MatchKey = MatchKey.(string) |
| 127 | + } |
| 128 | + |
| 129 | + if MatchValue, ok := d.GetOk("match_value"); ok { |
| 130 | + fvTagSelectorAttr.MatchValue = MatchValue.(string) |
| 131 | + } |
| 132 | + |
| 133 | + if Name, ok := d.GetOk("name"); ok { |
| 134 | + fvTagSelectorAttr.Name = Name.(string) |
| 135 | + } |
| 136 | + |
| 137 | + if ValueOperator, ok := d.GetOk("value_operator"); ok { |
| 138 | + fvTagSelectorAttr.ValueOperator = ValueOperator.(string) |
| 139 | + } |
| 140 | + fvTagSelector := models.NewEndpointSecurityGroupTagSelector(fmt.Sprintf("tagselectorkey-[%s]-value-[%s]", matchKey, matchValue), EndpointSecurityGroupDn, desc, nameAlias, fvTagSelectorAttr) |
| 141 | + |
| 142 | + err := aciClient.Save(fvTagSelector) |
| 143 | + if err != nil { |
| 144 | + return diag.FromErr(err) |
| 145 | + } |
| 146 | + |
| 147 | + d.SetId(fvTagSelector.DistinguishedName) |
| 148 | + log.Printf("[DEBUG] %s: Creation finished successfully", d.Id()) |
| 149 | + return resourceAciEndpointSecurityGroupTagSelectorRead(ctx, d, m) |
| 150 | +} |
| 151 | + |
| 152 | +func resourceAciEndpointSecurityGroupTagSelectorUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 153 | + log.Printf("[DEBUG] EndpointSecurityGroupTagSelector: Beginning Update") |
| 154 | + aciClient := m.(*client.Client) |
| 155 | + desc := d.Get("description").(string) |
| 156 | + matchKey := d.Get("match_key").(string) |
| 157 | + matchValue := d.Get("match_value").(string) |
| 158 | + EndpointSecurityGroupDn := d.Get("endpoint_security_group_dn").(string) |
| 159 | + fvTagSelectorAttr := models.EndpointSecurityGroupTagSelectorAttributes{} |
| 160 | + nameAlias := "" |
| 161 | + if NameAlias, ok := d.GetOk("name_alias"); ok { |
| 162 | + nameAlias = NameAlias.(string) |
| 163 | + } |
| 164 | + |
| 165 | + if Annotation, ok := d.GetOk("annotation"); ok { |
| 166 | + fvTagSelectorAttr.Annotation = Annotation.(string) |
| 167 | + } else { |
| 168 | + fvTagSelectorAttr.Annotation = "{}" |
| 169 | + } |
| 170 | + |
| 171 | + if MatchKey, ok := d.GetOk("match_key"); ok { |
| 172 | + fvTagSelectorAttr.MatchKey = MatchKey.(string) |
| 173 | + } |
| 174 | + |
| 175 | + if MatchValue, ok := d.GetOk("match_value"); ok { |
| 176 | + fvTagSelectorAttr.MatchValue = MatchValue.(string) |
| 177 | + } |
| 178 | + |
| 179 | + if Name, ok := d.GetOk("name"); ok { |
| 180 | + fvTagSelectorAttr.Name = Name.(string) |
| 181 | + } |
| 182 | + |
| 183 | + if ValueOperator, ok := d.GetOk("value_operator"); ok { |
| 184 | + fvTagSelectorAttr.ValueOperator = ValueOperator.(string) |
| 185 | + } |
| 186 | + fvTagSelector := models.NewEndpointSecurityGroupTagSelector(fmt.Sprintf("tagselectorkey-[%s]-value-[%s]", matchKey, matchValue), EndpointSecurityGroupDn, desc, nameAlias, fvTagSelectorAttr) |
| 187 | + |
| 188 | + fvTagSelector.Status = "modified" |
| 189 | + err := aciClient.Save(fvTagSelector) |
| 190 | + if err != nil { |
| 191 | + return diag.FromErr(err) |
| 192 | + } |
| 193 | + |
| 194 | + d.SetId(fvTagSelector.DistinguishedName) |
| 195 | + log.Printf("[DEBUG] %s: Update finished successfully", d.Id()) |
| 196 | + return resourceAciEndpointSecurityGroupTagSelectorRead(ctx, d, m) |
| 197 | +} |
| 198 | + |
| 199 | +func resourceAciEndpointSecurityGroupTagSelectorRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 200 | + log.Printf("[DEBUG] %s: Beginning Read", d.Id()) |
| 201 | + aciClient := m.(*client.Client) |
| 202 | + dn := d.Id() |
| 203 | + fvTagSelector, err := getRemoteEndpointSecurityGroupTagSelector(aciClient, dn) |
| 204 | + if err != nil { |
| 205 | + d.SetId("") |
| 206 | + return diag.FromErr(err) |
| 207 | + } |
| 208 | + _, err = setEndpointSecurityGroupTagSelectorAttributes(fvTagSelector, d) |
| 209 | + if err != nil { |
| 210 | + d.SetId("") |
| 211 | + return diag.FromErr(err) |
| 212 | + } |
| 213 | + |
| 214 | + log.Printf("[DEBUG] %s: Read finished successfully", d.Id()) |
| 215 | + return nil |
| 216 | +} |
| 217 | + |
| 218 | +func resourceAciEndpointSecurityGroupTagSelectorDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 219 | + log.Printf("[DEBUG] %s: Beginning Destroy", d.Id()) |
| 220 | + aciClient := m.(*client.Client) |
| 221 | + dn := d.Id() |
| 222 | + err := aciClient.DeleteByDn(dn, "fvTagSelector") |
| 223 | + if err != nil { |
| 224 | + return diag.FromErr(err) |
| 225 | + } |
| 226 | + log.Printf("[DEBUG] %s: Destroy finished successfully", d.Id()) |
| 227 | + d.SetId("") |
| 228 | + return diag.FromErr(err) |
| 229 | +} |
0 commit comments