|
| 1 | +package openstack |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "log" |
| 6 | + |
| 7 | + "github.com/gophercloud/gophercloud/openstack/blockstorage/v3/volumetypes" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 9 | +) |
| 10 | + |
| 11 | +func resourceBlockStorageVolumeTypeV3() *schema.Resource { |
| 12 | + return &schema.Resource{ |
| 13 | + Create: resourceBlockStorageVolumeTypeV3Create, |
| 14 | + Read: resourceBlockStorageVolumeTypeV3Read, |
| 15 | + Update: resourceBlockStorageVolumeTypeV3Update, |
| 16 | + Delete: resourceBlockStorageVolumeTypeV3Delete, |
| 17 | + Importer: &schema.ResourceImporter{ |
| 18 | + State: schema.ImportStatePassthrough, |
| 19 | + }, |
| 20 | + |
| 21 | + Schema: map[string]*schema.Schema{ |
| 22 | + "region": { |
| 23 | + Type: schema.TypeString, |
| 24 | + Optional: true, |
| 25 | + Computed: true, |
| 26 | + ForceNew: true, |
| 27 | + }, |
| 28 | + |
| 29 | + "name": { |
| 30 | + Type: schema.TypeString, |
| 31 | + Required: true, |
| 32 | + }, |
| 33 | + |
| 34 | + "description": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Optional: true, |
| 37 | + Computed: true, |
| 38 | + }, |
| 39 | + |
| 40 | + "is_public": { |
| 41 | + Type: schema.TypeBool, |
| 42 | + Optional: true, |
| 43 | + Computed: true, |
| 44 | + }, |
| 45 | + |
| 46 | + "extra_specs": { |
| 47 | + Type: schema.TypeMap, |
| 48 | + Optional: true, |
| 49 | + Computed: true, |
| 50 | + }, |
| 51 | + }, |
| 52 | + } |
| 53 | +} |
| 54 | + |
| 55 | +func resourceBlockStorageVolumeTypeV3Create(d *schema.ResourceData, meta interface{}) error { |
| 56 | + config := meta.(*Config) |
| 57 | + blockStorageClient, err := config.BlockStorageV3Client(GetRegion(d, config)) |
| 58 | + if err != nil { |
| 59 | + return fmt.Errorf("Error creating OpenStack block storage client: %s", err) |
| 60 | + } |
| 61 | + |
| 62 | + name := d.Get("name").(string) |
| 63 | + description := d.Get("description").(string) |
| 64 | + isPublic := d.Get("is_public").(bool) |
| 65 | + extraSpecs := d.Get("extra_specs").(map[string]interface{}) |
| 66 | + createOpts := volumetypes.CreateOpts{ |
| 67 | + Name: name, |
| 68 | + Description: description, |
| 69 | + IsPublic: &isPublic, |
| 70 | + ExtraSpecs: expandToMapStringString(extraSpecs), |
| 71 | + } |
| 72 | + |
| 73 | + log.Printf("[DEBUG] openstack_blockstorage_volume_type_v3 create options: %#v", createOpts) |
| 74 | + vt, err := volumetypes.Create(blockStorageClient, &createOpts).Extract() |
| 75 | + if err != nil { |
| 76 | + return fmt.Errorf("Error creating openstack_blockstorage_volume_type_v3 %s: %s", name, err) |
| 77 | + } |
| 78 | + |
| 79 | + d.SetId(vt.ID) |
| 80 | + |
| 81 | + return resourceBlockStorageVolumeTypeV3Read(d, meta) |
| 82 | +} |
| 83 | + |
| 84 | +func resourceBlockStorageVolumeTypeV3Read(d *schema.ResourceData, meta interface{}) error { |
| 85 | + config := meta.(*Config) |
| 86 | + blockStorageClient, err := config.BlockStorageV3Client(GetRegion(d, config)) |
| 87 | + if err != nil { |
| 88 | + return fmt.Errorf("Error creating OpenStack block storage client: %s", err) |
| 89 | + } |
| 90 | + |
| 91 | + vt, err := volumetypes.Get(blockStorageClient, d.Id()).Extract() |
| 92 | + if err != nil { |
| 93 | + return CheckDeleted(d, err, "Error retrieving openstack_blockstorage_volume_type_v3") |
| 94 | + } |
| 95 | + |
| 96 | + log.Printf("[DEBUG] Retrieved openstack_blockstorage_volume_type_v3 %s: %#v", d.Id(), vt) |
| 97 | + |
| 98 | + d.Set("name", vt.Name) |
| 99 | + d.Set("description", vt.Description) |
| 100 | + d.Set("is_public", vt.IsPublic) |
| 101 | + d.Set("region", GetRegion(d, config)) |
| 102 | + |
| 103 | + es, err := volumetypes.ListExtraSpecs(blockStorageClient, d.Id()).Extract() |
| 104 | + if err != nil { |
| 105 | + return fmt.Errorf("Error reading extra_specs for openstack_blockstorage_volume_type_v3 %s: %s", d.Id(), err) |
| 106 | + } |
| 107 | + |
| 108 | + if err := d.Set("extra_specs", es); err != nil { |
| 109 | + log.Printf("[WARN] Unable to set extra_specs for openstack_blockstorage_volume_type_v3 %s: %s", d.Id(), err) |
| 110 | + } |
| 111 | + |
| 112 | + return nil |
| 113 | +} |
| 114 | + |
| 115 | +func resourceBlockStorageVolumeTypeV3Update(d *schema.ResourceData, meta interface{}) error { |
| 116 | + config := meta.(*Config) |
| 117 | + blockStorageClient, err := config.BlockStorageV3Client(GetRegion(d, config)) |
| 118 | + if err != nil { |
| 119 | + return fmt.Errorf("Error creating OpenStack block storage client: %s", err) |
| 120 | + } |
| 121 | + |
| 122 | + hasChange := false |
| 123 | + var updateOpts volumetypes.UpdateOpts |
| 124 | + |
| 125 | + if d.HasChange("name") { |
| 126 | + hasChange = true |
| 127 | + name := d.Get("name").(string) |
| 128 | + updateOpts.Name = &name |
| 129 | + } |
| 130 | + |
| 131 | + if d.HasChange("description") { |
| 132 | + hasChange = true |
| 133 | + description := d.Get("description").(string) |
| 134 | + updateOpts.Description = &description |
| 135 | + } |
| 136 | + |
| 137 | + if d.HasChange("is_public") { |
| 138 | + hasChange = true |
| 139 | + isPublic := d.Get("is_public").(bool) |
| 140 | + updateOpts.IsPublic = &isPublic |
| 141 | + } |
| 142 | + |
| 143 | + if hasChange { |
| 144 | + _, err = volumetypes.Update(blockStorageClient, d.Id(), updateOpts).Extract() |
| 145 | + if err != nil { |
| 146 | + return fmt.Errorf("Error updating openstack_blockstorage_volume_type_v3 %s: %s", d.Id(), err) |
| 147 | + } |
| 148 | + } |
| 149 | + |
| 150 | + if d.HasChange("extra_specs") { |
| 151 | + oldES, newES := d.GetChange("extra_specs") |
| 152 | + |
| 153 | + // Delete all old extra specs. |
| 154 | + for oldKey := range oldES.(map[string]interface{}) { |
| 155 | + if err := volumetypes.DeleteExtraSpec(blockStorageClient, d.Id(), oldKey).ExtractErr(); err != nil { |
| 156 | + return fmt.Errorf("Error deleting extra_spec %s from openstack_blockstorage_volume_type_v3 %s: %s", oldKey, d.Id(), err) |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + // Add new extra specs. |
| 161 | + newESRaw := newES.(map[string]interface{}) |
| 162 | + if len(newESRaw) > 0 { |
| 163 | + extraSpecs := expandBlockStorageVolumeTypeV3ExtraSpecs(newESRaw) |
| 164 | + |
| 165 | + _, err := volumetypes.CreateExtraSpecs(blockStorageClient, d.Id(), extraSpecs).Extract() |
| 166 | + if err != nil { |
| 167 | + return fmt.Errorf("Error creating extra_specs for openstack_blockstorage_volume_type_v3 %s: %s", d.Id(), err) |
| 168 | + } |
| 169 | + } |
| 170 | + } |
| 171 | + |
| 172 | + return resourceBlockStorageVolumeTypeV3Read(d, meta) |
| 173 | +} |
| 174 | + |
| 175 | +func resourceBlockStorageVolumeTypeV3Delete(d *schema.ResourceData, meta interface{}) error { |
| 176 | + config := meta.(*Config) |
| 177 | + blockStorageClient, err := config.BlockStorageV3Client(GetRegion(d, config)) |
| 178 | + if err != nil { |
| 179 | + return fmt.Errorf("Error creating OpenStack block storage client: %s", err) |
| 180 | + } |
| 181 | + |
| 182 | + err = volumetypes.Delete(blockStorageClient, d.Id()).ExtractErr() |
| 183 | + if err != nil { |
| 184 | + return CheckDeleted(d, err, "Error deleting openstack_blockstorage_volume_type_v3") |
| 185 | + } |
| 186 | + |
| 187 | + return nil |
| 188 | +} |
0 commit comments