-
Notifications
You must be signed in to change notification settings - Fork 7
/
resource_location.go
127 lines (104 loc) · 2.63 KB
/
resource_location.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
package main
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/adeleporte/terraform-provider-hcx/hcx"
)
func resourceLocation() *schema.Resource {
return &schema.Resource{
CreateContext: resourceLocationCreate,
ReadContext: resourceLocationRead,
UpdateContext: resourceLocationUpdate,
DeleteContext: resourceLocationDelete,
Schema: map[string]*schema.Schema{
"city": {
Type: schema.TypeString,
Optional: true,
Default: "",
},
"country": {
Type: schema.TypeString,
Optional: true,
Default: "",
},
"cityascii": {
Type: schema.TypeString,
Computed: true,
},
"latitude": {
Type: schema.TypeFloat,
Optional: true,
Default: 0,
},
"longitude": {
Type: schema.TypeFloat,
Optional: true,
Default: 0,
},
"province": {
Type: schema.TypeString,
Optional: true,
Default: "",
},
},
}
}
func resourceLocationCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
return resourceLocationUpdate(ctx, d, m)
}
func resourceLocationRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client := m.(*hcx.Client)
resp, err := hcx.GetLocation(client)
if err != nil {
return diag.FromErr(err)
}
d.SetId(resp.City)
d.Set("cityascii", resp.City)
d.Set("country", resp.Country)
d.Set("province", resp.Province)
d.Set("latitude", resp.Latitude)
d.Set("longitude", resp.Longitude)
return diags
}
func resourceLocationUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
client := m.(*hcx.Client)
city := d.Get("city").(string)
country := d.Get("country").(string)
cityAscii := city
latitude := d.Get("latitude").(float64)
longitude := d.Get("longitude").(float64)
province := d.Get("province").(string)
body := hcx.SetLocationBody{
City: city,
Country: country,
CityAscii: cityAscii,
Latitude: latitude,
Longitude: longitude,
Province: province,
}
err := hcx.SetLocation(client, body)
if err != nil {
return diag.FromErr(err)
}
d.SetId(city)
return resourceLocationRead(ctx, d, m)
}
func resourceLocationDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client := m.(*hcx.Client)
body := hcx.SetLocationBody{
City: "",
Country: "",
CityAscii: "",
Latitude: 0,
Longitude: 0,
Province: "",
}
err := hcx.SetLocation(client, body)
if err != nil {
return diag.FromErr(err)
}
return diags
}