|
| 1 | +package cloudstack |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + "strconv" |
| 7 | + "strings" |
| 8 | + |
| 9 | + "github.com/hashicorp/terraform/helper/schema" |
| 10 | + "github.com/xanzy/go-cloudstack/cloudstack" |
| 11 | +) |
| 12 | + |
| 13 | +func resourceCloudStackSecurityGroup() *schema.Resource { |
| 14 | + return &schema.Resource{ |
| 15 | + Create: resourceCloudStackSecurityGroupCreate, |
| 16 | + Read: resourceCloudStackSecurityGroupRead, |
| 17 | + Delete: resourceCloudStackSecurityGroupDelete, |
| 18 | + |
| 19 | + Schema: map[string]*schema.Schema{ |
| 20 | + "name": &schema.Schema{ |
| 21 | + Type: schema.TypeString, |
| 22 | + Required: true, |
| 23 | + ForceNew: true, |
| 24 | + }, |
| 25 | + |
| 26 | + "description": &schema.Schema{ |
| 27 | + Type: schema.TypeString, |
| 28 | + Optional: true, |
| 29 | + Computed: true, |
| 30 | + ForceNew: true, |
| 31 | + }, |
| 32 | + |
| 33 | + "project": &schema.Schema{ |
| 34 | + Type: schema.TypeString, |
| 35 | + Optional: true, |
| 36 | + ForceNew: true, |
| 37 | + }, |
| 38 | + |
| 39 | + "rules": &schema.Schema{ |
| 40 | + Type: schema.TypeList, |
| 41 | + ForceNew: true, |
| 42 | + Optional: true, |
| 43 | + Elem: &schema.Resource{ |
| 44 | + Schema: map[string]*schema.Schema{ |
| 45 | + "cidr_list": &schema.Schema{ |
| 46 | + Type: schema.TypeString, |
| 47 | + Optional: true, |
| 48 | + ForceNew: true, |
| 49 | + }, |
| 50 | + |
| 51 | + "security_group": &schema.Schema{ |
| 52 | + Type: schema.TypeString, |
| 53 | + Optional: true, |
| 54 | + ForceNew: true, |
| 55 | + }, |
| 56 | + |
| 57 | + "protocol": &schema.Schema{ |
| 58 | + Type: schema.TypeString, |
| 59 | + Required: true, |
| 60 | + ForceNew: true, |
| 61 | + }, |
| 62 | + |
| 63 | + "ports": &schema.Schema{ |
| 64 | + Type: schema.TypeString, |
| 65 | + Optional: true, |
| 66 | + ForceNew: true, |
| 67 | + }, |
| 68 | + |
| 69 | + "traffic_type": &schema.Schema{ |
| 70 | + Type: schema.TypeString, |
| 71 | + Optional: true, |
| 72 | + Default: "ingress", |
| 73 | + }, |
| 74 | + }, |
| 75 | + }, |
| 76 | + }, |
| 77 | + }, |
| 78 | + } |
| 79 | +} |
| 80 | + |
| 81 | +func resourceCloudStackSecurityGroupCreate(d *schema.ResourceData, meta interface{}) error { |
| 82 | + cs := meta.(*cloudstack.CloudStackClient) |
| 83 | + |
| 84 | + name := d.Get("name").(string) |
| 85 | + |
| 86 | + // Create a new parameter struct |
| 87 | + p := cs.SecurityGroup.NewCreateSecurityGroupParams(name) |
| 88 | + |
| 89 | + // Set the description |
| 90 | + if description, ok := d.GetOk("description"); ok { |
| 91 | + p.SetDescription(description.(string)) |
| 92 | + } else { |
| 93 | + p.SetDescription(name) |
| 94 | + } |
| 95 | + |
| 96 | + // If there is a project supplied, we retrieve and set the project id |
| 97 | + if err := setProjectid(p, cs, d); err != nil { |
| 98 | + return err |
| 99 | + } |
| 100 | + |
| 101 | + log.Printf("[DEBUG] Creating security group %s", name) |
| 102 | + r, err := cs.SecurityGroup.CreateSecurityGroup(p) |
| 103 | + if err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + |
| 107 | + log.Printf("[DEBUG] Security group %s successfully created with ID: %s", name, r.Id) |
| 108 | + d.SetId(r.Id) |
| 109 | + |
| 110 | + // Create Rules |
| 111 | + rules := d.Get("rules").([]interface{}) |
| 112 | + for _, r := range rules { |
| 113 | + |
| 114 | + rule := r.(map[string]interface{}) |
| 115 | + |
| 116 | + m := splitPorts.FindStringSubmatch(rule["ports"].(string)) |
| 117 | + startPort, err := strconv.Atoi(m[1]) |
| 118 | + if err != nil { |
| 119 | + return err |
| 120 | + } |
| 121 | + |
| 122 | + endPort := startPort |
| 123 | + if m[2] != "" { |
| 124 | + endPort, err = strconv.Atoi(m[2]) |
| 125 | + if err != nil { |
| 126 | + return err |
| 127 | + } |
| 128 | + } |
| 129 | + |
| 130 | + traffic := rule["traffic_type"].(string) |
| 131 | + if traffic == "ingress" { |
| 132 | + param := cs.SecurityGroup.NewAuthorizeSecurityGroupIngressParams() |
| 133 | + |
| 134 | + param.SetSecuritygroupid(d.Id()) |
| 135 | + if cidrlist := rule["cidr_list"]; cidrlist != "" { |
| 136 | + param.SetCidrlist([]string{cidrlist.(string)}) |
| 137 | + log.Printf("[DEBUG] cidr = %v", cidrlist) |
| 138 | + } |
| 139 | + if securitygroup := rule["security_group"]; securitygroup != "" { |
| 140 | + // Get the security group details |
| 141 | + ag, count, err := cs.SecurityGroup.GetSecurityGroupByName( |
| 142 | + securitygroup.(string), |
| 143 | + cloudstack.WithProject(d.Get("project").(string)), |
| 144 | + ) |
| 145 | + if err != nil { |
| 146 | + if count == 0 { |
| 147 | + log.Printf("[DEBUG] Security group %s does not longer exist", d.Get("name").(string)) |
| 148 | + d.SetId("") |
| 149 | + return nil |
| 150 | + } |
| 151 | + |
| 152 | + log.Printf("[DEBUG] Found %v groups matching", count) |
| 153 | + |
| 154 | + return err |
| 155 | + } |
| 156 | + log.Printf("[DEBUG] ag = %v", ag) |
| 157 | + m := make(map[string]string) |
| 158 | + m[ag.Account] = ag.Name |
| 159 | + log.Printf("[DEBUG] m = %v", m) |
| 160 | + param.SetUsersecuritygrouplist(m) |
| 161 | + } |
| 162 | + param.SetStartport(startPort) |
| 163 | + param.SetEndport(endPort) |
| 164 | + param.SetProtocol(rule["protocol"].(string)) |
| 165 | + |
| 166 | + log.Printf("[DEBUG] Authorizing Ingress Rule %#v", param) |
| 167 | + _, err := cs.SecurityGroup.AuthorizeSecurityGroupIngress(param) |
| 168 | + if err != nil { |
| 169 | + return err |
| 170 | + } |
| 171 | + } else if traffic == "egress" { |
| 172 | + param := cs.SecurityGroup.NewAuthorizeSecurityGroupEgressParams() |
| 173 | + |
| 174 | + param.SetSecuritygroupid(d.Id()) |
| 175 | + if cidrlist := rule["cidr_list"]; cidrlist != "" { |
| 176 | + param.SetCidrlist([]string{cidrlist.(string)}) |
| 177 | + log.Printf("[DEBUG] cidr = %v", cidrlist) |
| 178 | + } |
| 179 | + if securitygroup := rule["security_group"]; securitygroup != "" { |
| 180 | + // Get the security group details |
| 181 | + ag, count, err := cs.SecurityGroup.GetSecurityGroupByName( |
| 182 | + securitygroup.(string), |
| 183 | + cloudstack.WithProject(d.Get("project").(string)), |
| 184 | + ) |
| 185 | + if err != nil { |
| 186 | + if count == 0 { |
| 187 | + log.Printf("[DEBUG] Security group %s does not longer exist", d.Get("name").(string)) |
| 188 | + d.SetId("") |
| 189 | + return nil |
| 190 | + } |
| 191 | + |
| 192 | + log.Printf("[DEBUG] Found %v groups matching", count) |
| 193 | + |
| 194 | + return err |
| 195 | + } |
| 196 | + log.Printf("[DEBUG] ag = %v", ag) |
| 197 | + m := make(map[string]string) |
| 198 | + m[ag.Account] = ag.Name |
| 199 | + log.Printf("[DEBUG] m = %v", m) |
| 200 | + param.SetUsersecuritygrouplist(m) |
| 201 | + } |
| 202 | + param.SetStartport(startPort) |
| 203 | + param.SetEndport(endPort) |
| 204 | + param.SetProtocol(rule["protocol"].(string)) |
| 205 | + |
| 206 | + log.Printf("[DEBUG] Authorizing Egress Rule %#v", param) |
| 207 | + _, err := cs.SecurityGroup.AuthorizeSecurityGroupEgress(param) |
| 208 | + if err != nil { |
| 209 | + return err |
| 210 | + } |
| 211 | + } else { |
| 212 | + return fmt.Errorf( |
| 213 | + "Parameter traffic_type only accepts 'ingress' or 'egress' as values") |
| 214 | + } |
| 215 | + } |
| 216 | + |
| 217 | + return resourceCloudStackSecurityGroupRead(d, meta) |
| 218 | +} |
| 219 | + |
| 220 | +func resourceCloudStackSecurityGroupRead(d *schema.ResourceData, meta interface{}) error { |
| 221 | + cs := meta.(*cloudstack.CloudStackClient) |
| 222 | + |
| 223 | + log.Printf("[DEBUG] Retrieving security group %s (ID=%s)", d.Get("name").(string), d.Id()) |
| 224 | + |
| 225 | + // Get the security group details |
| 226 | + ag, count, err := cs.SecurityGroup.GetSecurityGroupByID( |
| 227 | + d.Id(), |
| 228 | + cloudstack.WithProject(d.Get("project").(string)), |
| 229 | + ) |
| 230 | + if err != nil { |
| 231 | + if count == 0 { |
| 232 | + log.Printf("[DEBUG] Security group %s does not longer exist", d.Get("name").(string)) |
| 233 | + d.SetId("") |
| 234 | + return nil |
| 235 | + } |
| 236 | + |
| 237 | + log.Printf("[DEBUG] Found %v groups matching", count) |
| 238 | + |
| 239 | + return err |
| 240 | + } |
| 241 | + |
| 242 | + // Update the config |
| 243 | + d.Set("name", ag.Name) |
| 244 | + d.Set("description", ag.Description) |
| 245 | + |
| 246 | + var rules []interface{} |
| 247 | + for _, r := range ag.Ingressrule { |
| 248 | + var ports string |
| 249 | + if r.Startport == r.Endport { |
| 250 | + ports = strconv.Itoa(r.Startport) |
| 251 | + } else { |
| 252 | + ports = fmt.Sprintf("%v-%v", r.Startport, r.Endport) |
| 253 | + } |
| 254 | + rule := map[string]interface{}{ |
| 255 | + "cidr_list": r.Cidr, |
| 256 | + "ports": ports, |
| 257 | + "protocol": r.Protocol, |
| 258 | + "traffic_type": "ingress", |
| 259 | + "security_group": r.Securitygroupname, |
| 260 | + } |
| 261 | + rules = append(rules, rule) |
| 262 | + } |
| 263 | + for _, r := range ag.Egressrule { |
| 264 | + var ports string |
| 265 | + if r.Startport == r.Endport { |
| 266 | + ports = strconv.Itoa(r.Startport) |
| 267 | + } else { |
| 268 | + ports = fmt.Sprintf("%s-%s", r.Startport, r.Endport) |
| 269 | + } |
| 270 | + rule := map[string]interface{}{ |
| 271 | + "cidr_list": r.Cidr, |
| 272 | + "ports": ports, |
| 273 | + "protocol": r.Protocol, |
| 274 | + "traffic_type": "egress", |
| 275 | + "security_group": r.Securitygroupname, |
| 276 | + } |
| 277 | + rules = append(rules, rule) |
| 278 | + } |
| 279 | + d.Set("rules", rules) |
| 280 | + |
| 281 | + return nil |
| 282 | +} |
| 283 | + |
| 284 | +func resourceCloudStackSecurityGroupDelete(d *schema.ResourceData, meta interface{}) error { |
| 285 | + cs := meta.(*cloudstack.CloudStackClient) |
| 286 | + |
| 287 | + // Create a new parameter struct |
| 288 | + p := cs.SecurityGroup.NewDeleteSecurityGroupParams() |
| 289 | + p.SetId(d.Id()) |
| 290 | + |
| 291 | + // If there is a project supplied, we retrieve and set the project id |
| 292 | + if err := setProjectid(p, cs, d); err != nil { |
| 293 | + return err |
| 294 | + } |
| 295 | + |
| 296 | + // Delete the security group |
| 297 | + _, err := cs.SecurityGroup.DeleteSecurityGroup(p) |
| 298 | + if err != nil { |
| 299 | + // This is a very poor way to be told the ID does no longer exist :( |
| 300 | + if strings.Contains(err.Error(), fmt.Sprintf( |
| 301 | + "Invalid parameter id value=%s due to incorrect long value format, "+ |
| 302 | + "or entity does not exist", d.Id())) { |
| 303 | + return nil |
| 304 | + } |
| 305 | + |
| 306 | + return fmt.Errorf("Error deleting security group: %s", err) |
| 307 | + } |
| 308 | + |
| 309 | + return nil |
| 310 | +} |
0 commit comments