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