forked from scaleway/terraform-provider-scaleway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsource.go
143 lines (130 loc) · 4.35 KB
/
source.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
package cockpit
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/scaleway/scaleway-sdk-go/api/cockpit/v1"
"github.com/scaleway/scaleway-sdk-go/scw"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/httperrors"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/locality/regional"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/services/account"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/types"
"github.com/scaleway/terraform-provider-scaleway/v2/internal/verify"
)
func ResourceCockpitSource() *schema.Resource {
return &schema.Resource{
CreateContext: ResourceCockpitSourceCreate,
ReadContext: ResourceCockpitSourceRead,
DeleteContext: ResourceCockpitSourceDelete,
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(DefaultCockpitTimeout),
Read: schema.DefaultTimeout(DefaultCockpitTimeout),
Delete: schema.DefaultTimeout(DefaultCockpitTimeout),
Default: schema.DefaultTimeout(DefaultCockpitTimeout),
},
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "Name of the datasource",
},
"type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "The type of the datasource",
ValidateDiagFunc: verify.ValidateEnum[cockpit.DataSourceType](),
},
// computed
"url": {
Type: schema.TypeString,
Computed: true,
Description: "The URL of the datasource",
},
"origin": {
Type: schema.TypeString,
Computed: true,
Description: "The origin of the datasource",
},
"synchronized_with_grafana": {
Type: schema.TypeBool,
Computed: true,
Description: "Indicates whether the data source is synchronized with Grafana",
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Description: "The date and time of the creation of the cockpit datasource",
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
Description: "The date and time of the last update of the cockpit datasource",
},
"project_id": account.ProjectIDSchema(),
"region": regional.Schema(),
},
}
}
func ResourceCockpitSourceCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
api, region, err := cockpitAPIWithRegion(d, meta)
if err != nil {
return diag.FromErr(err)
}
res, err := api.CreateDataSource(&cockpit.RegionalAPICreateDataSourceRequest{
Region: region,
ProjectID: d.Get("project_id").(string),
Name: d.Get("name").(string),
Type: cockpit.DataSourceType(d.Get("type").(string)),
}, scw.WithContext(ctx))
if err != nil {
return diag.FromErr(err)
}
d.SetId(regional.NewIDString(region, res.ID))
return ResourceCockpitSourceRead(ctx, d, meta)
}
func ResourceCockpitSourceRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
api, region, id, err := NewAPIWithRegionAndID(meta, d.Id())
if err != nil {
return diag.FromErr(err)
}
res, err := api.GetDataSource(&cockpit.RegionalAPIGetDataSourceRequest{
Region: region,
DataSourceID: id,
}, scw.WithContext(ctx))
if err != nil {
if httperrors.Is404(err) {
d.SetId("")
return nil
}
return diag.FromErr(err)
}
_ = d.Set("name", res.Name)
_ = d.Set("type", res.Type.String())
_ = d.Set("url", res.URL)
_ = d.Set("origin", res.Origin)
_ = d.Set("synchronized_with_grafana", res.SynchronizedWithGrafana)
_ = d.Set("region", res.Region)
_ = d.Set("created_at", types.FlattenTime(res.CreatedAt))
_ = d.Set("updated_at", types.FlattenTime(res.UpdatedAt))
_ = d.Set("project_id", res.ProjectID)
return nil
}
func ResourceCockpitSourceDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
api, region, id, err := NewAPIWithRegionAndID(meta, d.Id())
if err != nil {
return diag.FromErr(err)
}
err = api.DeleteDataSource(&cockpit.RegionalAPIDeleteDataSourceRequest{
DataSourceID: id,
Region: region,
}, scw.WithContext(ctx))
if err != nil && !httperrors.Is404(err) {
return diag.FromErr(err)
}
return nil
}