This repository has been archived by the owner on Sep 12, 2020. It is now read-only.
forked from yandex-cloud/terraform-provider-yandex
-
Notifications
You must be signed in to change notification settings - Fork 6
/
data_source_yandex_resourcemanager_cloud.go
89 lines (77 loc) · 2.08 KB
/
data_source_yandex_resourcemanager_cloud.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
package yandex
import (
"context"
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"google.golang.org/grpc/codes"
"github.com/yandex-cloud/go-genproto/yandex/cloud/resourcemanager/v1"
)
func dataSourceYandexResourceManagerCloud() *schema.Resource {
return &schema.Resource{
Read: dataSourceYandexResourceManagerCloudRead,
Schema: map[string]*schema.Schema{
"cloud_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"name"},
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ConflictsWith: []string{"cloud_id"},
},
"description": {
Type: schema.TypeString,
Optional: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceYandexResourceManagerCloudRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
var cloud *resourcemanager.Cloud
if v, ok := d.GetOk("cloud_id"); ok {
resp, err := config.sdk.ResourceManager().Cloud().Get(context.Background(), &resourcemanager.GetCloudRequest{
CloudId: v.(string),
})
if err != nil {
if isStatusWithCode(err, codes.NotFound) {
return fmt.Errorf("cloud not found: %s", v)
}
return err
}
cloud = resp
} else if v, ok := d.GetOk("name"); ok {
filter := fmt.Sprintf(`name = "%s"`, v.(string))
resp, err := config.sdk.ResourceManager().Cloud().List(context.Background(), &resourcemanager.ListCloudsRequest{
Filter: filter,
})
if err != nil {
return fmt.Errorf("error reading cloud: %s", err)
}
if len(resp.Clouds) == 0 {
return fmt.Errorf("cloud not found: %s", v)
}
if len(resp.Clouds) > 1 {
return fmt.Errorf("more than one matching cloud found")
}
cloud = resp.Clouds[0]
} else {
return fmt.Errorf("one of 'cloud_id' or 'name' must be set")
}
createdAt, err := getTimestamp(cloud.CreatedAt)
if err != nil {
return err
}
d.Set("name", cloud.Name)
d.Set("description", cloud.Description)
d.Set("created_at", createdAt)
d.SetId(cloud.Id)
return nil
}