forked from scaleway/terraform-provider-scaleway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplan_data_source.go
54 lines (44 loc) · 1.15 KB
/
plan_data_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
package cockpit
import (
"context"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
cockpit "github.com/scaleway/scaleway-sdk-go/api/cockpit/v1beta1"
"github.com/scaleway/scaleway-sdk-go/scw"
)
func DataSourcePlan() *schema.Resource {
return &schema.Resource{
ReadContext: DataSourceCockpitPlanRead,
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Description: "The name of the plan",
Required: true,
},
},
}
}
func DataSourceCockpitPlanRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
api, err := NewAPI(m)
if err != nil {
return diag.FromErr(err)
}
name := d.Get("name").(string)
res, err := api.ListPlans(&cockpit.ListPlansRequest{}, scw.WithContext(ctx), scw.WithAllPages())
if err != nil {
return diag.FromErr(err)
}
var plan *cockpit.Plan
for _, p := range res.Plans {
if p.Name.String() == name {
plan = p
break
}
}
if plan == nil {
return diag.Errorf("could not find plan with name %s", name)
}
d.SetId(plan.ID)
_ = d.Set("name", plan.Name.String())
return nil
}