github.com/richardbowden/terraform@v0.6.12-0.20160901200758-30ea22c25211/builtin/providers/scaleway/resource_scaleway_security_group.go (about) 1 package scaleway 2 3 import ( 4 "fmt" 5 "log" 6 7 "github.com/hashicorp/terraform/helper/schema" 8 "github.com/scaleway/scaleway-cli/pkg/api" 9 ) 10 11 func resourceScalewaySecurityGroup() *schema.Resource { 12 return &schema.Resource{ 13 Create: resourceScalewaySecurityGroupCreate, 14 Read: resourceScalewaySecurityGroupRead, 15 Update: resourceScalewaySecurityGroupUpdate, 16 Delete: resourceScalewaySecurityGroupDelete, 17 Schema: map[string]*schema.Schema{ 18 "name": &schema.Schema{ 19 Type: schema.TypeString, 20 Required: true, 21 }, 22 "description": &schema.Schema{ 23 Type: schema.TypeString, 24 Required: true, 25 }, 26 }, 27 } 28 } 29 30 func resourceScalewaySecurityGroupCreate(d *schema.ResourceData, m interface{}) error { 31 scaleway := m.(*Client).scaleway 32 33 req := api.ScalewayNewSecurityGroup{ 34 Name: d.Get("name").(string), 35 Description: d.Get("description").(string), 36 Organization: scaleway.Organization, 37 } 38 39 err := scaleway.PostSecurityGroup(req) 40 if err != nil { 41 if serr, ok := err.(api.ScalewayAPIError); ok { 42 log.Printf("[DEBUG] Error creating security group: %q\n", serr.APIMessage) 43 } 44 45 return err 46 } 47 48 resp, err := scaleway.GetSecurityGroups() 49 if err != nil { 50 return err 51 } 52 53 for _, group := range resp.SecurityGroups { 54 if group.Name == req.Name { 55 d.SetId(group.ID) 56 break 57 } 58 } 59 60 if d.Id() == "" { 61 return fmt.Errorf("Failed to find created security group.") 62 } 63 64 return resourceScalewaySecurityGroupRead(d, m) 65 } 66 67 func resourceScalewaySecurityGroupRead(d *schema.ResourceData, m interface{}) error { 68 scaleway := m.(*Client).scaleway 69 resp, err := scaleway.GetASecurityGroup(d.Id()) 70 71 if err != nil { 72 if serr, ok := err.(api.ScalewayAPIError); ok { 73 log.Printf("[DEBUG] Error reading security group: %q\n", serr.APIMessage) 74 75 if serr.StatusCode == 404 { 76 d.SetId("") 77 return nil 78 } 79 } 80 81 return err 82 } 83 84 d.Set("name", resp.SecurityGroups.Name) 85 d.Set("description", resp.SecurityGroups.Description) 86 87 return nil 88 } 89 90 func resourceScalewaySecurityGroupUpdate(d *schema.ResourceData, m interface{}) error { 91 scaleway := m.(*Client).scaleway 92 93 var req = api.ScalewayUpdateSecurityGroup{ 94 Organization: scaleway.Organization, 95 Name: d.Get("name").(string), 96 Description: d.Get("description").(string), 97 } 98 99 if err := scaleway.PutSecurityGroup(req, d.Id()); err != nil { 100 log.Printf("[DEBUG] Error reading security group: %q\n", err) 101 102 return err 103 } 104 105 return resourceScalewaySecurityGroupRead(d, m) 106 } 107 108 func resourceScalewaySecurityGroupDelete(d *schema.ResourceData, m interface{}) error { 109 scaleway := m.(*Client).scaleway 110 111 err := scaleway.DeleteSecurityGroup(d.Id()) 112 if err != nil { 113 if serr, ok := err.(api.ScalewayAPIError); ok { 114 log.Printf("[DEBUG] error reading Security Group Rule: %q\n", serr.APIMessage) 115 116 if serr.StatusCode == 404 { 117 d.SetId("") 118 return nil 119 } 120 } 121 122 return err 123 } 124 125 d.SetId("") 126 return nil 127 }