|
| 1 | +package aci |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "reflect" |
| 7 | + "sort" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/ciscoecosystem/aci-go-client/client" |
| 11 | + "github.com/ciscoecosystem/aci-go-client/models" |
| 12 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 13 | + "github.com/hashicorp/terraform-plugin-sdk/helper/validation" |
| 14 | +) |
| 15 | + |
| 16 | +func resourceAciSpanningTreeInterfacePolicy() *schema.Resource { |
| 17 | + |
| 18 | + return &schema.Resource{ |
| 19 | + Create: resourceAciSpanningTreeInterfacePolicyCreate, |
| 20 | + Update: resourceAciSpanningTreeInterfacePolicyUpdate, |
| 21 | + Read: resourceAciSpanningTreeInterfacePolicyRead, |
| 22 | + Delete: resourceAciSpanningTreeInterfacePolicyDelete, |
| 23 | + |
| 24 | + Importer: &schema.ResourceImporter{ |
| 25 | + State: resourceAciSpanningTreeInterfacePolicyImport, |
| 26 | + }, |
| 27 | + |
| 28 | + SchemaVersion: 1, |
| 29 | + Schema: AppendBaseAttrSchema(AppendNameAliasAttrSchema(map[string]*schema.Schema{ |
| 30 | + "name": &schema.Schema{ |
| 31 | + Type: schema.TypeString, |
| 32 | + Required: true, |
| 33 | + ForceNew: true, |
| 34 | + }, |
| 35 | + "ctrl": &schema.Schema{ |
| 36 | + Type: schema.TypeList, |
| 37 | + Optional: true, |
| 38 | + Computed: true, |
| 39 | + Elem: &schema.Schema{ |
| 40 | + Type: schema.TypeString, |
| 41 | + ValidateFunc: validation.StringInSlice([]string{ |
| 42 | + "bpdu-filter", |
| 43 | + "bpdu-guard", |
| 44 | + }, false), |
| 45 | + }, |
| 46 | + }, |
| 47 | + })), |
| 48 | + } |
| 49 | +} |
| 50 | + |
| 51 | +func getRemoteSpanningTreeInterfacePolicy(client *client.Client, dn string) (*models.SpanningTreeInterfacePolicy, error) { |
| 52 | + stpIfPolCont, err := client.Get(dn) |
| 53 | + if err != nil { |
| 54 | + return nil, err |
| 55 | + } |
| 56 | + stpIfPol := models.SpanningTreeInterfacePolicyFromContainer(stpIfPolCont) |
| 57 | + if stpIfPol.DistinguishedName == "" { |
| 58 | + return nil, fmt.Errorf("SpanningTreeInterfacePolicy %s not found", stpIfPol.DistinguishedName) |
| 59 | + } |
| 60 | + return stpIfPol, nil |
| 61 | +} |
| 62 | + |
| 63 | +func setSpanningTreeInterfacePolicyAttributes(stpIfPol *models.SpanningTreeInterfacePolicy, d *schema.ResourceData) *schema.ResourceData { |
| 64 | + d.SetId(stpIfPol.DistinguishedName) |
| 65 | + d.Set("description", stpIfPol.Description) |
| 66 | + stpIfPolMap, _ := stpIfPol.ToMap() |
| 67 | + d.Set("name", stpIfPolMap["name"]) |
| 68 | + d.Set("annotation", stpIfPolMap["annotation"]) |
| 69 | + ctrlGet := make([]string, 0, 1) |
| 70 | + for _, val := range strings.Split(stpIfPolMap["ctrl"], ",") { |
| 71 | + ctrlGet = append(ctrlGet, strings.Trim(val, " ")) |
| 72 | + } |
| 73 | + sort.Strings(ctrlGet) |
| 74 | + if ctrlIntr, ok := d.GetOk("ctrl"); ok { |
| 75 | + ctrlAct := make([]string, 0, 1) |
| 76 | + for _, val := range ctrlIntr.([]interface{}) { |
| 77 | + ctrlAct = append(ctrlAct, val.(string)) |
| 78 | + } |
| 79 | + sort.Strings(ctrlAct) |
| 80 | + if reflect.DeepEqual(ctrlAct, ctrlGet) { |
| 81 | + d.Set("ctrl", d.Get("ctrl").([]interface{})) |
| 82 | + } else { |
| 83 | + d.Set("ctrl", ctrlGet) |
| 84 | + } |
| 85 | + } else { |
| 86 | + d.Set("ctrl", ctrlGet) |
| 87 | + } |
| 88 | + d.Set("name_alias", stpIfPolMap["nameAlias"]) |
| 89 | + return d |
| 90 | +} |
| 91 | + |
| 92 | +func resourceAciSpanningTreeInterfacePolicyImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) { |
| 93 | + log.Printf("[DEBUG] %s: Beginning Import", d.Id()) |
| 94 | + aciClient := m.(*client.Client) |
| 95 | + dn := d.Id() |
| 96 | + stpIfPol, err := getRemoteSpanningTreeInterfacePolicy(aciClient, dn) |
| 97 | + if err != nil { |
| 98 | + return nil, err |
| 99 | + } |
| 100 | + schemaFilled := setSpanningTreeInterfacePolicyAttributes(stpIfPol, d) |
| 101 | + log.Printf("[DEBUG] %s: Import finished successfully", d.Id()) |
| 102 | + return []*schema.ResourceData{schemaFilled}, nil |
| 103 | +} |
| 104 | + |
| 105 | +func resourceAciSpanningTreeInterfacePolicyCreate(d *schema.ResourceData, m interface{}) error { |
| 106 | + return resourceAciSpanningTreeInterfacePolicyCreateOrUpdate(d, m, false) |
| 107 | +} |
| 108 | + |
| 109 | +func resourceAciSpanningTreeInterfacePolicyUpdate(d *schema.ResourceData, m interface{}) error { |
| 110 | + return resourceAciSpanningTreeInterfacePolicyCreateOrUpdate(d, m, true) |
| 111 | +} |
| 112 | + |
| 113 | +func resourceAciSpanningTreeInterfacePolicyRead(d *schema.ResourceData, m interface{}) error { |
| 114 | + log.Printf("[DEBUG] %s: Beginning Read", d.Id()) |
| 115 | + aciClient := m.(*client.Client) |
| 116 | + dn := d.Id() |
| 117 | + stpIfPol, err := getRemoteSpanningTreeInterfacePolicy(aciClient, dn) |
| 118 | + if err != nil { |
| 119 | + d.SetId("") |
| 120 | + return nil |
| 121 | + } |
| 122 | + setSpanningTreeInterfacePolicyAttributes(stpIfPol, d) |
| 123 | + log.Printf("[DEBUG] %s: Read finished successfully", d.Id()) |
| 124 | + return nil |
| 125 | +} |
| 126 | + |
| 127 | +func resourceAciSpanningTreeInterfacePolicyDelete(d *schema.ResourceData, m interface{}) error { |
| 128 | + log.Printf("[DEBUG] %s: Beginning Destroy", d.Id()) |
| 129 | + aciClient := m.(*client.Client) |
| 130 | + dn := d.Id() |
| 131 | + err := aciClient.DeleteByDn(dn, "stpIfPol") |
| 132 | + if err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + log.Printf("[DEBUG] %s: Destroy finished successfully", d.Id()) |
| 136 | + d.SetId("") |
| 137 | + return err |
| 138 | +} |
| 139 | +func resourceAciSpanningTreeInterfacePolicyCreateOrUpdate(d *schema.ResourceData, m interface{}, update bool) error { |
| 140 | + action := "Creation" |
| 141 | + if update == true { |
| 142 | + action = "Update" |
| 143 | + } |
| 144 | + log.Printf("[DEBUG] SpanningTreeInterfacePolicy: Beginning %s", action) |
| 145 | + aciClient := m.(*client.Client) |
| 146 | + desc := d.Get("description").(string) |
| 147 | + name := d.Get("name").(string) |
| 148 | + stpIfPolAttr := models.SpanningTreeInterfacePolicyAttributes{} |
| 149 | + if Annotation, ok := d.GetOk("annotation"); ok { |
| 150 | + stpIfPolAttr.Annotation = Annotation.(string) |
| 151 | + } else { |
| 152 | + stpIfPolAttr.Annotation = "{}" |
| 153 | + } |
| 154 | + if Ctrl, ok := d.GetOk("ctrl"); ok { |
| 155 | + ctrlList := make([]string, 0, 1) |
| 156 | + for _, val := range Ctrl.([]interface{}) { |
| 157 | + ctrlList = append(ctrlList, val.(string)) |
| 158 | + } |
| 159 | + Ctrl := strings.Join(ctrlList, ",") |
| 160 | + stpIfPolAttr.Ctrl = Ctrl |
| 161 | + } |
| 162 | + nameAlias := "" |
| 163 | + if NameAlias, ok := d.GetOk("name_alias"); ok { |
| 164 | + nameAlias = NameAlias.(string) |
| 165 | + } |
| 166 | + |
| 167 | + stpIfPol := models.NewSpanningTreeInterfacePolicy(fmt.Sprintf("infra/ifPol-%s", name), "uni", desc, nameAlias, stpIfPolAttr) |
| 168 | + if update == true { |
| 169 | + stpIfPol.Status = "modified" |
| 170 | + } |
| 171 | + err := aciClient.Save(stpIfPol) |
| 172 | + if err != nil { |
| 173 | + return err |
| 174 | + } |
| 175 | + d.Partial(true) |
| 176 | + d.SetPartial("name") |
| 177 | + d.Partial(false) |
| 178 | + d.SetId(stpIfPol.DistinguishedName) |
| 179 | + log.Printf("[DEBUG] %s: %s finished successfully", d.Id(), action) |
| 180 | + return resourceAciSpanningTreeInterfacePolicyRead(d, m) |
| 181 | +} |
0 commit comments