github.com/danp/terraform@v0.9.5-0.20170426144147-39d740081351/builtin/providers/opc/resource_storage_volume_snapshot.go (about) 1 package opc 2 3 import ( 4 "fmt" 5 "strconv" 6 7 "github.com/hashicorp/go-oracle-terraform/compute" 8 "github.com/hashicorp/terraform/helper/schema" 9 ) 10 11 func resourceOPCStorageVolumeSnapshot() *schema.Resource { 12 return &schema.Resource{ 13 Create: resourceOPCStorageVolumeSnapshotCreate, 14 Read: resourceOPCStorageVolumeSnapshotRead, 15 Delete: resourceOPCStorageVolumeSnapshotDelete, 16 Importer: &schema.ResourceImporter{ 17 State: schema.ImportStatePassthrough, 18 }, 19 20 Schema: map[string]*schema.Schema{ 21 // Required Attributes 22 "volume_name": { 23 Type: schema.TypeString, 24 Required: true, 25 ForceNew: true, 26 }, 27 28 // Optional Attributes 29 "description": { 30 Type: schema.TypeString, 31 Optional: true, 32 ForceNew: true, 33 }, 34 35 // Optional, but also computed if unspecified 36 "name": { 37 Type: schema.TypeString, 38 Optional: true, 39 Computed: true, 40 ForceNew: true, 41 }, 42 43 "parent_volume_bootable": { 44 Type: schema.TypeBool, 45 Optional: true, 46 ForceNew: true, 47 Default: false, 48 }, 49 50 "collocated": { 51 Type: schema.TypeBool, 52 Optional: true, 53 Default: false, 54 ForceNew: true, 55 }, 56 57 "tags": tagsForceNewSchema(), 58 59 // Computed Attributes 60 "account": { 61 Type: schema.TypeString, 62 Computed: true, 63 }, 64 65 "machine_image_name": { 66 Type: schema.TypeString, 67 Computed: true, 68 }, 69 70 "size": { 71 Type: schema.TypeString, 72 Computed: true, 73 }, 74 75 "property": { 76 Type: schema.TypeString, 77 Computed: true, 78 }, 79 80 "platform": { 81 Type: schema.TypeString, 82 Computed: true, 83 }, 84 85 "snapshot_timestamp": { 86 Type: schema.TypeString, 87 Computed: true, 88 }, 89 90 "snapshot_id": { 91 Type: schema.TypeString, 92 Computed: true, 93 }, 94 95 "start_timestamp": { 96 Type: schema.TypeString, 97 Computed: true, 98 }, 99 100 "status": { 101 Type: schema.TypeString, 102 Computed: true, 103 }, 104 105 "status_detail": { 106 Type: schema.TypeString, 107 Computed: true, 108 }, 109 110 "status_timestamp": { 111 Type: schema.TypeString, 112 Computed: true, 113 }, 114 115 "uri": { 116 Type: schema.TypeString, 117 Computed: true, 118 }, 119 }, 120 } 121 } 122 123 func resourceOPCStorageVolumeSnapshotCreate(d *schema.ResourceData, meta interface{}) error { 124 client := meta.(*compute.Client).StorageVolumeSnapshots() 125 126 // Get required attribute 127 input := &compute.CreateStorageVolumeSnapshotInput{ 128 Volume: d.Get("volume_name").(string), 129 } 130 131 if v, ok := d.GetOk("description"); ok { 132 input.Description = v.(string) 133 } 134 135 if v, ok := d.GetOk("name"); ok { 136 input.Name = v.(string) 137 } 138 139 // Convert parent_volume_bootable to string 140 bootable := d.Get("parent_volume_bootable").(bool) 141 if bootable { 142 input.ParentVolumeBootable = "true" 143 } 144 145 collocated := d.Get("collocated").(bool) 146 if collocated { 147 input.Property = compute.SnapshotPropertyCollocated 148 } 149 150 tags := getStringList(d, "tags") 151 if len(tags) > 0 { 152 input.Tags = tags 153 } 154 155 info, err := client.CreateStorageVolumeSnapshot(input) 156 if err != nil { 157 return fmt.Errorf("Error creating snapshot '%s': %v", input.Name, err) 158 } 159 160 d.SetId(info.Name) 161 return resourceOPCStorageVolumeSnapshotRead(d, meta) 162 } 163 164 func resourceOPCStorageVolumeSnapshotRead(d *schema.ResourceData, meta interface{}) error { 165 client := meta.(*compute.Client).StorageVolumeSnapshots() 166 167 name := d.Id() 168 input := &compute.GetStorageVolumeSnapshotInput{ 169 Name: name, 170 } 171 172 result, err := client.GetStorageVolumeSnapshot(input) 173 if err != nil { 174 if compute.WasNotFoundError(err) { 175 d.SetId("") 176 return nil 177 } 178 return fmt.Errorf("Error reading storage volume snapshot '%s': %v", name, err) 179 } 180 181 d.Set("volume_name", result.Volume) 182 d.Set("description", result.Description) 183 d.Set("name", result.Name) 184 d.Set("property", result.Property) 185 d.Set("platform", result.Platform) 186 d.Set("account", result.Account) 187 d.Set("machine_image_name", result.MachineImageName) 188 d.Set("size", result.Size) 189 d.Set("snapshot_timestamp", result.SnapshotTimestamp) 190 d.Set("snapshot_id", result.SnapshotID) 191 d.Set("start_timestamp", result.StartTimestamp) 192 d.Set("status", result.Status) 193 d.Set("status_detail", result.StatusDetail) 194 d.Set("status_timestamp", result.StatusTimestamp) 195 d.Set("uri", result.URI) 196 197 bootable, err := strconv.ParseBool(result.ParentVolumeBootable) 198 if err != nil { 199 return fmt.Errorf("Error converting parent volume to boolean: %v", err) 200 } 201 d.Set("parent_volume_bootable", bootable) 202 203 if result.Property != compute.SnapshotPropertyCollocated { 204 d.Set("collocated", false) 205 } else { 206 d.Set("collocated", true) 207 } 208 209 if err := setStringList(d, "tags", result.Tags); err != nil { 210 return err 211 } 212 213 return nil 214 } 215 216 func resourceOPCStorageVolumeSnapshotDelete(d *schema.ResourceData, meta interface{}) error { 217 client := meta.(*compute.Client).StorageVolumeSnapshots() 218 219 name := d.Id() 220 221 input := &compute.DeleteStorageVolumeSnapshotInput{ 222 Name: name, 223 } 224 225 if err := client.DeleteStorageVolumeSnapshot(input); err != nil { 226 return fmt.Errorf("Error deleting storage volume snapshot '%s': %v", name, err) 227 } 228 229 return nil 230 }