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_folder.go
112 lines (94 loc) · 2.58 KB
/
data_source_yandex_resourcemanager_folder.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
package yandex
import (
"context"
"fmt"
"strings"
"time"
"github.com/hashicorp/terraform/helper/schema"
"github.com/yandex-cloud/go-genproto/yandex/cloud/resourcemanager/v1"
ycsdk "github.com/yandex-cloud/go-sdk"
"github.com/yandex-cloud/go-sdk/sdkresolvers"
)
const yandexResourceManagerFolderDefaultTimeout = 1 * time.Minute
func dataSourceYandexResourceManagerFolder() *schema.Resource {
return &schema.Resource{
Read: dataSourceYandexResourceManagerFolderRead,
Schema: map[string]*schema.Schema{
"folder_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"labels": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"cloud_id": {
Type: schema.TypeString,
Computed: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
type cloudID string
func (id cloudID) folderResolver(name string, opts ...sdkresolvers.ResolveOption) ycsdk.Resolver {
opts = append(opts, sdkresolvers.CloudID(string(id)))
return sdkresolvers.FolderResolver(name, opts...)
}
func dataSourceYandexResourceManagerFolderRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
ctx := context.Background()
err := checkOneOf(d, "folder_id", "name")
if err != nil {
return err
}
folderID := d.Get("folder_id").(string)
folderName, folderNameOk := d.GetOk("name")
if folderNameOk {
resolver := cloudID(config.CloudID).folderResolver
folderID, err = resolveObjectID(ctx, config, folderName.(string), resolver)
if err != nil {
return fmt.Errorf("failed to resolve data source folder by name: %v", err)
}
}
folder, err := config.sdk.ResourceManager().Folder().Get(context.Background(), &resourcemanager.GetFolderRequest{
FolderId: folderID,
})
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("folder with ID %q", folderID))
}
createdAt, err := getTimestamp(folder.CreatedAt)
if err != nil {
return err
}
d.Set("folder_id", folder.Id)
d.Set("name", folder.Name)
d.Set("description", folder.Description)
d.Set("cloud_id", folder.CloudId)
d.Set("status", strings.ToLower(folder.Status.String()))
d.Set("created_at", createdAt)
if err := d.Set("labels", folder.Labels); err != nil {
return err
}
d.SetId(folder.Id)
return nil
}