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_container_registry.go
104 lines (85 loc) · 2.21 KB
/
data_source_yandex_container_registry.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
package yandex
import (
"fmt"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"google.golang.org/grpc/codes"
"github.com/yandex-cloud/go-genproto/yandex/cloud/containerregistry/v1"
"github.com/yandex-cloud/go-sdk/sdkresolvers"
)
func dataSourceYandexContainerRegistry() *schema.Resource {
return &schema.Resource{
Read: dataSourceYandexContainerRegistryRead,
Schema: map[string]*schema.Schema{
"registry_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"folder_id": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"status": {
Type: schema.TypeString,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
"labels": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
},
}
}
func dataSourceYandexContainerRegistryRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
ctx := config.Context()
err := checkOneOf(d, "registry_id", "name")
if err != nil {
return err
}
registryID := d.Get("registry_id").(string)
_, registryNameOk := d.GetOk("name")
if registryNameOk {
registryID, err = resolveObjectID(ctx, config, d, sdkresolvers.RegistryResolver)
if err != nil {
return fmt.Errorf("failed to resolve container registry data source by name: %v", err)
}
}
registry, err := config.sdk.ContainerRegistry().Registry().Get(ctx,
&containerregistry.GetRegistryRequest{
RegistryId: registryID,
})
if err != nil {
if isStatusWithCode(err, codes.NotFound) {
return fmt.Errorf("container registry not found: %s", registryID)
}
return err
}
createdAt, err := getTimestamp(registry.CreatedAt)
if err != nil {
return err
}
d.Set("registry_id", registry.Id)
d.Set("folder_id", registry.FolderId)
d.Set("name", registry.Name)
d.Set("status", strings.ToLower(registry.Status.String()))
d.Set("created_at", createdAt)
if err := d.Set("labels", registry.Labels); err != nil {
return err
}
d.SetId(registry.Id)
return nil
}