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
121 lines (101 loc) · 2.78 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
113
114
115
116
117
118
119
120
121
package yandex
import (
"context"
"fmt"
"strings"
"time"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/yandex-cloud/go-genproto/yandex/cloud/resourcemanager/v1"
"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,
},
"cloud_id": {
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,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceYandexResourceManagerFolderRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
ctx := config.Context()
err := checkOneOf(d, "folder_id", "name")
if err != nil {
return err
}
folderID := d.Get("folder_id").(string)
folderName, folderNameOk := d.GetOk("name")
if folderNameOk {
dsCloudID, err := getCloudID(d, config)
if err != nil {
return fmt.Errorf("error getting cloud ID to resolve data source for folder: %s", err)
}
folderID, err = resolveFolderIDByName(ctx, config, folderName.(string), dsCloudID)
if err != nil {
return fmt.Errorf("failed to resolve data source folder by name: %v", err)
}
}
folder, err := config.sdk.ResourceManager().Folder().Get(ctx, &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
}
func resolveFolderIDByName(ctx context.Context, config *Config, folderName, cloudID string) (string, error) {
var objectID string
resolver := sdkresolvers.FolderResolver(folderName, sdkresolvers.CloudID(cloudID), sdkresolvers.Out(&objectID))
err := config.sdk.Resolve(ctx, resolver)
if err != nil {
return "", err
}
return objectID, nil
}