/
resource_bizflycloud_volume.go
158 lines (150 loc) · 4.38 KB
/
resource_bizflycloud_volume.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
// This file is part of terraform-provider-bizflycloud
//
// Copyright (C) 2021 Bizfly Cloud
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>
package bizflycloud
import (
"context"
"errors"
"fmt"
"log"
"time"
"github.com/bizflycloud/gobizfly"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)
func resourceBizflyCloudVolume() *schema.Resource {
return &schema.Resource{
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Create: resourceBizflyCloudVolumeCreate,
Read: resourceBizflyCloudVolumeRead,
Update: resourceBizflyCloudVolumeUpdate,
Delete: resourceBizflyCloudVolumeDelete,
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(20 * time.Minute),
},
SchemaVersion: 1,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"type": {
Type: schema.TypeString,
Required: true,
},
"size": {
Type: schema.TypeInt,
Required: true,
},
"category": {
Type: schema.TypeString,
Required: true,
},
"availability_zone": {
Type: schema.TypeString,
Required: true,
},
"user_id": {
Type: schema.TypeString,
Computed: true,
},
"project_id": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func resourceBizflyCloudVolumeCreate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*CombinedConfig).gobizflyClient()
vcr := &gobizfly.VolumeCreateRequest{
Name: d.Get("name").(string),
Size: d.Get("size").(int),
VolumeType: d.Get("type").(string),
VolumeCategory: d.Get("category").(string),
AvailabilityZone: d.Get("availability_zone").(string),
}
log.Printf("[DEBUG] Create Volume configuration #{vcr}")
volume, err := client.Volume.Create(context.Background(), vcr)
if err != nil {
return fmt.Errorf("Error creating volume: %v", err)
}
d.SetId(volume.ID)
err = resourceBizflyCloudVolumeRead(d, meta)
if err != nil {
return fmt.Errorf("Error retrieving volume: %v", err)
}
return nil
}
func resourceBizflyCloudVolumeRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*CombinedConfig).gobizflyClient()
volume, err := client.Volume.Get(context.Background(), d.Id())
if err != nil {
if errors.Is(err, gobizfly.ErrNotFound) {
log.Printf("[WARN] VOlume (%s) is not found", d.Id())
d.SetId("")
return nil
}
return fmt.Errorf("Error retrieving volume: %v", err)
}
_ = d.Set("name", volume.Name)
_ = d.Set("size", volume.Size)
_ = d.Set("status", volume.Status)
_ = d.Set("type", volume.VolumeType)
_ = d.Set("category", volume.Category)
_ = d.Set("created_at", volume.CreatedAt)
_ = d.Set("availability_zone", volume.AvailabilityZone)
_ = d.Set("user_id", volume.UserID)
_ = d.Set("project_id", volume.ProjectID)
return nil
}
func resourceBizflyCloudVolumeUpdate(d *schema.ResourceData, meta interface{}) error {
client := meta.(*CombinedConfig).gobizflyClient()
if d.HasChange("size") {
// resize volume
// TODO check state of extending task
_, err := client.Volume.ExtendVolume(context.Background(), d.Id(), d.Get("size").(int))
if err != nil {
if errors.Is(err, gobizfly.ErrNotFound) {
log.Printf("[WARN] Volume is not found %s", d.Id())
d.SetId("")
return nil
}
}
}
return nil
}
func resourceBizflyCloudVolumeDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*CombinedConfig).gobizflyClient()
err := client.Volume.Delete(context.Background(), d.Id())
if err != nil {
return fmt.Errorf("Error delete volume: #{err}")
}
return nil
}