|
| 1 | +package aci |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + |
| 7 | + "github.com/ciscoecosystem/aci-go-client/client" |
| 8 | + "github.com/ciscoecosystem/aci-go-client/models" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 10 | +) |
| 11 | + |
| 12 | +func resourceAciEndpointSecurityGroupSelector() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + Create: resourceAciEndpointSecurityGroupSelectorCreate, |
| 15 | + Update: resourceAciEndpointSecurityGroupSelectorUpdate, |
| 16 | + Read: resourceAciEndpointSecurityGroupSelectorRead, |
| 17 | + Delete: resourceAciEndpointSecurityGroupSelectorDelete, |
| 18 | + |
| 19 | + Importer: &schema.ResourceImporter{ |
| 20 | + State: resourceAciEndpointSecurityGroupSelectorImport, |
| 21 | + }, |
| 22 | + |
| 23 | + SchemaVersion: 1, |
| 24 | + Schema: AppendBaseAttrSchema(AppendNameAliasAttrSchema(map[string]*schema.Schema{ |
| 25 | + "endpoint_security_group_dn": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Required: true, |
| 28 | + ForceNew: true, |
| 29 | + }, |
| 30 | + "match_expression": { |
| 31 | + Type: schema.TypeString, |
| 32 | + Required: true, |
| 33 | + ForceNew: true, |
| 34 | + }, |
| 35 | + "name": { |
| 36 | + Type: schema.TypeString, |
| 37 | + Optional: true, |
| 38 | + Computed: true, |
| 39 | + }, |
| 40 | + })), |
| 41 | + } |
| 42 | +} |
| 43 | + |
| 44 | +func getRemoteEndpointSecurityGroupSelector(client *client.Client, dn string) (*models.EndpointSecurityGroupSelector, error) { |
| 45 | + fvEPSelectorCont, err := client.Get(dn) |
| 46 | + if err != nil { |
| 47 | + return nil, err |
| 48 | + } |
| 49 | + fvEPSelector := models.EndpointSecurityGroupSelectorFromContainer(fvEPSelectorCont) |
| 50 | + if fvEPSelector.DistinguishedName == "" { |
| 51 | + return nil, fmt.Errorf("EndpointSecurityGroupSelector %s not found", fvEPSelector.DistinguishedName) |
| 52 | + } |
| 53 | + return fvEPSelector, nil |
| 54 | +} |
| 55 | + |
| 56 | +func setEndpointSecurityGroupSelectorAttributes(fvEPSelector *models.EndpointSecurityGroupSelector, d *schema.ResourceData) *schema.ResourceData { |
| 57 | + d.SetId(fvEPSelector.DistinguishedName) |
| 58 | + d.Set("description", fvEPSelector.Description) |
| 59 | + fvEPSelectorMap, _ := fvEPSelector.ToMap() |
| 60 | + d.Set("endpoint_security_group_dn", GetParentDn(fvEPSelector.DistinguishedName, fmt.Sprintf("/epselector-[%s]"))) |
| 61 | + d.Set("annotation", fvEPSelectorMap["annotation"]) |
| 62 | + d.Set("match_expression", fvEPSelectorMap["matchExpression"]) |
| 63 | + d.Set("name", fvEPSelectorMap["name"]) |
| 64 | + d.Set("name_alias", fvEPSelectorMap["nameAlias"]) |
| 65 | + return d |
| 66 | +} |
| 67 | + |
| 68 | +func resourceAciEndpointSecurityGroupSelectorImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) { |
| 69 | + log.Printf("[DEBUG] %s: Beginning Import", d.Id()) |
| 70 | + aciClient := m.(*client.Client) |
| 71 | + dn := d.Id() |
| 72 | + fvEPSelector, err := getRemoteEndpointSecurityGroupSelector(aciClient, dn) |
| 73 | + if err != nil { |
| 74 | + return nil, err |
| 75 | + } |
| 76 | + schemaFilled := setEndpointSecurityGroupSelectorAttributes(fvEPSelector, d) |
| 77 | + log.Printf("[DEBUG] %s: Import finished successfully", d.Id()) |
| 78 | + return []*schema.ResourceData{schemaFilled}, nil |
| 79 | +} |
| 80 | + |
| 81 | +func resourceAciEndpointSecurityGroupSelectorCreate(d *schema.ResourceData, m interface{}) error { |
| 82 | + log.Printf("[DEBUG] EndpointSecurityGroupSelector: Beginning Creation") |
| 83 | + aciClient := m.(*client.Client) |
| 84 | + desc := d.Get("description").(string) |
| 85 | + matchExpression := d.Get("match_expression").(string) |
| 86 | + EndpointSecurityGroupDn := d.Get("endpoint_security_group_dn").(string) |
| 87 | + |
| 88 | + fvEPSelectorAttr := models.EndpointSecurityGroupSelectorAttributes{} |
| 89 | + nameAlias := "" |
| 90 | + if NameAlias, ok := d.GetOk("name_alias"); ok { |
| 91 | + nameAlias = NameAlias.(string) |
| 92 | + } |
| 93 | + if Annotation, ok := d.GetOk("annotation"); ok { |
| 94 | + fvEPSelectorAttr.Annotation = Annotation.(string) |
| 95 | + } else { |
| 96 | + fvEPSelectorAttr.Annotation = "{}" |
| 97 | + } |
| 98 | + |
| 99 | + if MatchExpression, ok := d.GetOk("match_expression"); ok { |
| 100 | + fvEPSelectorAttr.MatchExpression = MatchExpression.(string) |
| 101 | + } |
| 102 | + |
| 103 | + if Name, ok := d.GetOk("name"); ok { |
| 104 | + fvEPSelectorAttr.Name = Name.(string) |
| 105 | + } |
| 106 | + fvEPSelector := models.NewEndpointSecurityGroupSelector(fmt.Sprintf("epselector-[%s]", matchExpression), EndpointSecurityGroupDn, desc, nameAlias, fvEPSelectorAttr) |
| 107 | + |
| 108 | + err := aciClient.Save(fvEPSelector) |
| 109 | + if err != nil { |
| 110 | + return err |
| 111 | + } |
| 112 | + d.Partial(true) |
| 113 | + d.SetPartial("name") |
| 114 | + d.Partial(false) |
| 115 | + |
| 116 | + d.SetId(fvEPSelector.DistinguishedName) |
| 117 | + log.Printf("[DEBUG] %s: Creation finished successfully", d.Id()) |
| 118 | + return resourceAciEndpointSecurityGroupSelectorRead(d, m) |
| 119 | +} |
| 120 | + |
| 121 | +func resourceAciEndpointSecurityGroupSelectorUpdate(d *schema.ResourceData, m interface{}) error { |
| 122 | + log.Printf("[DEBUG] EndpointSecurityGroupSelector: Beginning Update") |
| 123 | + aciClient := m.(*client.Client) |
| 124 | + desc := d.Get("description").(string) |
| 125 | + matchExpression := d.Get("match_expression").(string) |
| 126 | + EndpointSecurityGroupDn := d.Get("endpoint_security_group_dn").(string) |
| 127 | + fvEPSelectorAttr := models.EndpointSecurityGroupSelectorAttributes{} |
| 128 | + nameAlias := "" |
| 129 | + if NameAlias, ok := d.GetOk("name_alias"); ok { |
| 130 | + nameAlias = NameAlias.(string) |
| 131 | + } |
| 132 | + |
| 133 | + if Annotation, ok := d.GetOk("annotation"); ok { |
| 134 | + fvEPSelectorAttr.Annotation = Annotation.(string) |
| 135 | + } else { |
| 136 | + fvEPSelectorAttr.Annotation = "{}" |
| 137 | + } |
| 138 | + |
| 139 | + if MatchExpression, ok := d.GetOk("match_expression"); ok { |
| 140 | + fvEPSelectorAttr.MatchExpression = MatchExpression.(string) |
| 141 | + } |
| 142 | + |
| 143 | + if Name, ok := d.GetOk("name"); ok { |
| 144 | + fvEPSelectorAttr.Name = Name.(string) |
| 145 | + } |
| 146 | + fvEPSelector := models.NewEndpointSecurityGroupSelector(fmt.Sprintf("epselector-[%s]", matchExpression), EndpointSecurityGroupDn, desc, nameAlias, fvEPSelectorAttr) |
| 147 | + |
| 148 | + fvEPSelector.Status = "modified" |
| 149 | + err := aciClient.Save(fvEPSelector) |
| 150 | + if err != nil { |
| 151 | + return err |
| 152 | + } |
| 153 | + d.Partial(true) |
| 154 | + d.SetPartial("name") |
| 155 | + d.Partial(false) |
| 156 | + |
| 157 | + d.SetId(fvEPSelector.DistinguishedName) |
| 158 | + log.Printf("[DEBUG] %s: Update finished successfully", d.Id()) |
| 159 | + return resourceAciEndpointSecurityGroupSelectorRead(d, m) |
| 160 | +} |
| 161 | + |
| 162 | +func resourceAciEndpointSecurityGroupSelectorRead(d *schema.ResourceData, m interface{}) error { |
| 163 | + log.Printf("[DEBUG] %s: Beginning Read", d.Id()) |
| 164 | + aciClient := m.(*client.Client) |
| 165 | + dn := d.Id() |
| 166 | + fvEPSelector, err := getRemoteEndpointSecurityGroupSelector(aciClient, dn) |
| 167 | + if err != nil { |
| 168 | + d.SetId("") |
| 169 | + return err |
| 170 | + } |
| 171 | + setEndpointSecurityGroupSelectorAttributes(fvEPSelector, d) |
| 172 | + |
| 173 | + log.Printf("[DEBUG] %s: Read finished successfully", d.Id()) |
| 174 | + return nil |
| 175 | +} |
| 176 | + |
| 177 | +func resourceAciEndpointSecurityGroupSelectorDelete(d *schema.ResourceData, m interface{}) error { |
| 178 | + log.Printf("[DEBUG] %s: Beginning Destroy", d.Id()) |
| 179 | + aciClient := m.(*client.Client) |
| 180 | + dn := d.Id() |
| 181 | + err := aciClient.DeleteByDn(dn, "fvEPSelector") |
| 182 | + if err != nil { |
| 183 | + return err |
| 184 | + } |
| 185 | + log.Printf("[DEBUG] %s: Destroy finished successfully", d.Id()) |
| 186 | + d.SetId("") |
| 187 | + return err |
| 188 | +} |
0 commit comments