github.com/tam7t/terraform@v0.7.0-rc2.0.20160705125922-be2469a05c5e/builtin/providers/openstack/resource_openstack_networking_secgroup_v2.go (about) 1 package openstack 2 3 import ( 4 "fmt" 5 "log" 6 "time" 7 8 "github.com/hashicorp/terraform/helper/resource" 9 "github.com/hashicorp/terraform/helper/schema" 10 11 "github.com/rackspace/gophercloud" 12 "github.com/rackspace/gophercloud/openstack/networking/v2/extensions/security/groups" 13 ) 14 15 func resourceNetworkingSecGroupV2() *schema.Resource { 16 return &schema.Resource{ 17 Create: resourceNetworkingSecGroupV2Create, 18 Read: resourceNetworkingSecGroupV2Read, 19 Delete: resourceNetworkingSecGroupV2Delete, 20 21 Schema: map[string]*schema.Schema{ 22 "region": &schema.Schema{ 23 Type: schema.TypeString, 24 Required: true, 25 ForceNew: true, 26 DefaultFunc: schema.EnvDefaultFunc("OS_REGION_NAME", ""), 27 }, 28 "name": &schema.Schema{ 29 Type: schema.TypeString, 30 Required: true, 31 ForceNew: true, 32 }, 33 "description": &schema.Schema{ 34 Type: schema.TypeString, 35 Optional: true, 36 ForceNew: true, 37 Computed: true, 38 }, 39 "tenant_id": &schema.Schema{ 40 Type: schema.TypeString, 41 Optional: true, 42 ForceNew: true, 43 Computed: true, 44 }, 45 }, 46 } 47 } 48 49 func resourceNetworkingSecGroupV2Create(d *schema.ResourceData, meta interface{}) error { 50 51 config := meta.(*Config) 52 networkingClient, err := config.networkingV2Client(d.Get("region").(string)) 53 if err != nil { 54 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 55 } 56 57 opts := groups.CreateOpts{ 58 Name: d.Get("name").(string), 59 Description: d.Get("description").(string), 60 TenantID: d.Get("tenant_id").(string), 61 } 62 63 log.Printf("[DEBUG] Create OpenStack Neutron Security Group: %#v", opts) 64 65 security_group, err := groups.Create(networkingClient, opts).Extract() 66 if err != nil { 67 return err 68 } 69 70 log.Printf("[DEBUG] OpenStack Neutron Security Group created: %#v", security_group) 71 72 d.SetId(security_group.ID) 73 74 return resourceNetworkingSecGroupV2Read(d, meta) 75 } 76 77 func resourceNetworkingSecGroupV2Read(d *schema.ResourceData, meta interface{}) error { 78 log.Printf("[DEBUG] Retrieve information about security group: %s", d.Id()) 79 80 config := meta.(*Config) 81 networkingClient, err := config.networkingV2Client(d.Get("region").(string)) 82 if err != nil { 83 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 84 } 85 86 security_group, err := groups.Get(networkingClient, d.Id()).Extract() 87 88 if err != nil { 89 return CheckDeleted(d, err, "OpenStack Neutron Security group") 90 } 91 92 d.Set("description", security_group.Description) 93 d.Set("tenant_id", security_group.TenantID) 94 return nil 95 } 96 97 func resourceNetworkingSecGroupV2Delete(d *schema.ResourceData, meta interface{}) error { 98 log.Printf("[DEBUG] Destroy security group: %s", d.Id()) 99 100 config := meta.(*Config) 101 networkingClient, err := config.networkingV2Client(d.Get("region").(string)) 102 if err != nil { 103 return fmt.Errorf("Error creating OpenStack networking client: %s", err) 104 } 105 106 stateConf := &resource.StateChangeConf{ 107 Pending: []string{"ACTIVE"}, 108 Target: []string{"DELETED"}, 109 Refresh: waitForSecGroupDelete(networkingClient, d.Id()), 110 Timeout: 2 * time.Minute, 111 Delay: 5 * time.Second, 112 MinTimeout: 3 * time.Second, 113 } 114 115 _, err = stateConf.WaitForState() 116 if err != nil { 117 return fmt.Errorf("Error deleting OpenStack Neutron Security Group: %s", err) 118 } 119 120 d.SetId("") 121 return err 122 } 123 124 func waitForSecGroupDelete(networkingClient *gophercloud.ServiceClient, secGroupId string) resource.StateRefreshFunc { 125 return func() (interface{}, string, error) { 126 log.Printf("[DEBUG] Attempting to delete OpenStack Security Group %s.\n", secGroupId) 127 128 r, err := groups.Get(networkingClient, secGroupId).Extract() 129 if err != nil { 130 errCode, ok := err.(*gophercloud.UnexpectedResponseCodeError) 131 if !ok { 132 return r, "ACTIVE", err 133 } 134 if errCode.Actual == 404 { 135 log.Printf("[DEBUG] Successfully deleted OpenStack Neutron Security Group %s", secGroupId) 136 return r, "DELETED", nil 137 } 138 } 139 140 err = groups.Delete(networkingClient, secGroupId).ExtractErr() 141 if err != nil { 142 errCode, ok := err.(*gophercloud.UnexpectedResponseCodeError) 143 if !ok { 144 return r, "ACTIVE", err 145 } 146 if errCode.Actual == 404 { 147 log.Printf("[DEBUG] Successfully deleted OpenStack Neutron Security Group %s", secGroupId) 148 return r, "DELETED", nil 149 } 150 } 151 152 log.Printf("[DEBUG] OpenStack Neutron Security Group %s still active.\n", secGroupId) 153 return r, "ACTIVE", nil 154 } 155 }