|
| 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 | +) |
| 13 | + |
| 14 | +func resourceAciEndpointSecurityGroupEPgSelector() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + CreateContext: resourceAciEndpointSecurityGroupEPgSelectorCreate, |
| 17 | + UpdateContext: resourceAciEndpointSecurityGroupEPgSelectorUpdate, |
| 18 | + ReadContext: resourceAciEndpointSecurityGroupEPgSelectorRead, |
| 19 | + DeleteContext: resourceAciEndpointSecurityGroupEPgSelectorDelete, |
| 20 | + |
| 21 | + Importer: &schema.ResourceImporter{ |
| 22 | + State: resourceAciEndpointSecurityGroupEPgSelectorImport, |
| 23 | + }, |
| 24 | + |
| 25 | + SchemaVersion: 1, |
| 26 | + Schema: AppendBaseAttrSchema(AppendNameAliasAttrSchema(map[string]*schema.Schema{ |
| 27 | + "endpoint_security_group_dn": { |
| 28 | + Type: schema.TypeString, |
| 29 | + Required: true, |
| 30 | + ForceNew: true, |
| 31 | + }, |
| 32 | + "match_epg_dn": { |
| 33 | + Type: schema.TypeString, |
| 34 | + Required: true, |
| 35 | + ForceNew: true, |
| 36 | + }, |
| 37 | + "name": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Optional: true, |
| 40 | + Computed: true, |
| 41 | + }, |
| 42 | + })), |
| 43 | + } |
| 44 | +} |
| 45 | + |
| 46 | +func getRemoteEndpointSecurityGroupEPgSelector(client *client.Client, dn string) (*models.EndpointSecurityGroupEPgSelector, error) { |
| 47 | + fvEPgSelectorCont, err := client.Get(dn) |
| 48 | + if err != nil { |
| 49 | + return nil, err |
| 50 | + } |
| 51 | + fvEPgSelector := models.EndpointSecurityGroupEPgSelectorFromContainer(fvEPgSelectorCont) |
| 52 | + if fvEPgSelector.DistinguishedName == "" { |
| 53 | + return nil, fmt.Errorf("EndpointSecurityGroupEPgSelector %s not found", fvEPgSelector.DistinguishedName) |
| 54 | + } |
| 55 | + return fvEPgSelector, nil |
| 56 | +} |
| 57 | + |
| 58 | +func setEndpointSecurityGroupEPgSelectorAttributes(fvEPgSelector *models.EndpointSecurityGroupEPgSelector, d *schema.ResourceData) (*schema.ResourceData, error) { |
| 59 | + d.SetId(fvEPgSelector.DistinguishedName) |
| 60 | + d.Set("description", fvEPgSelector.Description) |
| 61 | + fvEPgSelectorMap, err := fvEPgSelector.ToMap() |
| 62 | + if err != nil { |
| 63 | + return d, err |
| 64 | + } |
| 65 | + d.Set("annotation", fvEPgSelectorMap["annotation"]) |
| 66 | + d.Set("match_epg_dn", fvEPgSelectorMap["matchEpgDn"]) |
| 67 | + d.Set("name", fvEPgSelectorMap["name"]) |
| 68 | + d.Set("name_alias", fvEPgSelectorMap["nameAlias"]) |
| 69 | + return d, nil |
| 70 | +} |
| 71 | + |
| 72 | +func resourceAciEndpointSecurityGroupEPgSelectorImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) { |
| 73 | + log.Printf("[DEBUG] %s: Beginning Import", d.Id()) |
| 74 | + aciClient := m.(*client.Client) |
| 75 | + dn := d.Id() |
| 76 | + fvEPgSelector, err := getRemoteEndpointSecurityGroupEPgSelector(aciClient, dn) |
| 77 | + if err != nil { |
| 78 | + return nil, err |
| 79 | + } |
| 80 | + schemaFilled, err := setEndpointSecurityGroupEPgSelectorAttributes(fvEPgSelector, d) |
| 81 | + if err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + log.Printf("[DEBUG] %s: Import finished successfully", d.Id()) |
| 85 | + return []*schema.ResourceData{schemaFilled}, nil |
| 86 | +} |
| 87 | + |
| 88 | +func resourceAciEndpointSecurityGroupEPgSelectorCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 89 | + log.Printf("[DEBUG] EndpointSecurityGroupEPgSelector: Beginning Creation") |
| 90 | + aciClient := m.(*client.Client) |
| 91 | + desc := d.Get("description").(string) |
| 92 | + matchEpgDn := d.Get("match_epg_dn").(string) |
| 93 | + EndpointSecurityGroupDn := d.Get("endpoint_security_group_dn").(string) |
| 94 | + |
| 95 | + fvEPgSelectorAttr := models.EndpointSecurityGroupEPgSelectorAttributes{} |
| 96 | + nameAlias := "" |
| 97 | + if NameAlias, ok := d.GetOk("name_alias"); ok { |
| 98 | + nameAlias = NameAlias.(string) |
| 99 | + } |
| 100 | + if Annotation, ok := d.GetOk("annotation"); ok { |
| 101 | + fvEPgSelectorAttr.Annotation = Annotation.(string) |
| 102 | + } else { |
| 103 | + fvEPgSelectorAttr.Annotation = "{}" |
| 104 | + } |
| 105 | + |
| 106 | + if MatchEpgDn, ok := d.GetOk("match_epg_dn"); ok { |
| 107 | + fvEPgSelectorAttr.MatchEpgDn = MatchEpgDn.(string) |
| 108 | + } |
| 109 | + |
| 110 | + if Name, ok := d.GetOk("name"); ok { |
| 111 | + fvEPgSelectorAttr.Name = Name.(string) |
| 112 | + } |
| 113 | + fvEPgSelector := models.NewEndpointSecurityGroupEPgSelector(fmt.Sprintf("epgselector-[%s]", matchEpgDn), EndpointSecurityGroupDn, desc, nameAlias, fvEPgSelectorAttr) |
| 114 | + |
| 115 | + err := aciClient.Save(fvEPgSelector) |
| 116 | + if err != nil { |
| 117 | + return diag.FromErr(err) |
| 118 | + } |
| 119 | + d.SetId(fvEPgSelector.DistinguishedName) |
| 120 | + log.Printf("[DEBUG] %s: Creation finished successfully", d.Id()) |
| 121 | + return resourceAciEndpointSecurityGroupEPgSelectorRead(ctx, d, m) |
| 122 | +} |
| 123 | + |
| 124 | +func resourceAciEndpointSecurityGroupEPgSelectorUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 125 | + log.Printf("[DEBUG] EndpointSecurityGroupEPgSelector: Beginning Update") |
| 126 | + aciClient := m.(*client.Client) |
| 127 | + desc := d.Get("description").(string) |
| 128 | + matchEpgDn := d.Get("match_epg_dn").(string) |
| 129 | + EndpointSecurityGroupDn := d.Get("endpoint_security_group_dn").(string) |
| 130 | + fvEPgSelectorAttr := models.EndpointSecurityGroupEPgSelectorAttributes{} |
| 131 | + nameAlias := "" |
| 132 | + if NameAlias, ok := d.GetOk("name_alias"); ok { |
| 133 | + nameAlias = NameAlias.(string) |
| 134 | + } |
| 135 | + |
| 136 | + if Annotation, ok := d.GetOk("annotation"); ok { |
| 137 | + fvEPgSelectorAttr.Annotation = Annotation.(string) |
| 138 | + } else { |
| 139 | + fvEPgSelectorAttr.Annotation = "{}" |
| 140 | + } |
| 141 | + |
| 142 | + if MatchEpgDn, ok := d.GetOk("match_epg_dn"); ok { |
| 143 | + fvEPgSelectorAttr.MatchEpgDn = MatchEpgDn.(string) |
| 144 | + } |
| 145 | + |
| 146 | + if Name, ok := d.GetOk("name"); ok { |
| 147 | + fvEPgSelectorAttr.Name = Name.(string) |
| 148 | + } |
| 149 | + fvEPgSelector := models.NewEndpointSecurityGroupEPgSelector(fmt.Sprintf("epgselector-[%s]", matchEpgDn), EndpointSecurityGroupDn, desc, nameAlias, fvEPgSelectorAttr) |
| 150 | + |
| 151 | + fvEPgSelector.Status = "modified" |
| 152 | + err := aciClient.Save(fvEPgSelector) |
| 153 | + if err != nil { |
| 154 | + return diag.FromErr(err) |
| 155 | + } |
| 156 | + d.SetId(fvEPgSelector.DistinguishedName) |
| 157 | + log.Printf("[DEBUG] %s: Update finished successfully", d.Id()) |
| 158 | + return resourceAciEndpointSecurityGroupEPgSelectorRead(ctx, d, m) |
| 159 | +} |
| 160 | + |
| 161 | +func resourceAciEndpointSecurityGroupEPgSelectorRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 162 | + log.Printf("[DEBUG] %s: Beginning Read", d.Id()) |
| 163 | + aciClient := m.(*client.Client) |
| 164 | + dn := d.Id() |
| 165 | + fvEPgSelector, err := getRemoteEndpointSecurityGroupEPgSelector(aciClient, dn) |
| 166 | + if err != nil { |
| 167 | + d.SetId("") |
| 168 | + return diag.FromErr(err) |
| 169 | + } |
| 170 | + _, err = setEndpointSecurityGroupEPgSelectorAttributes(fvEPgSelector, d) |
| 171 | + if err != nil { |
| 172 | + d.SetId("") |
| 173 | + return nil |
| 174 | + } |
| 175 | + log.Printf("[DEBUG] %s: Read finished successfully", d.Id()) |
| 176 | + return nil |
| 177 | +} |
| 178 | + |
| 179 | +func resourceAciEndpointSecurityGroupEPgSelectorDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics { |
| 180 | + log.Printf("[DEBUG] %s: Beginning Destroy", d.Id()) |
| 181 | + aciClient := m.(*client.Client) |
| 182 | + dn := d.Id() |
| 183 | + err := aciClient.DeleteByDn(dn, "fvEPgSelector") |
| 184 | + if err != nil { |
| 185 | + return diag.FromErr(err) |
| 186 | + } |
| 187 | + log.Printf("[DEBUG] %s: Destroy finished successfully", d.Id()) |
| 188 | + d.SetId("") |
| 189 | + return diag.FromErr(err) |
| 190 | +} |
0 commit comments