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_lb_target_group.go
125 lines (102 loc) · 2.52 KB
/
data_source_yandex_lb_target_group.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
122
123
124
125
package yandex
import (
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/yandex-cloud/go-genproto/yandex/cloud/loadbalancer/v1"
"github.com/yandex-cloud/go-sdk/sdkresolvers"
)
func dataSourceYandexLBTargetGroup() *schema.Resource {
return &schema.Resource{
Read: dataSourceYandexLBTargetGroupRead,
Schema: map[string]*schema.Schema{
"target_group_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"folder_id": {
Type: schema.TypeString,
Computed: true,
},
"labels": {
Type: schema.TypeMap,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Set: schema.HashString,
},
"target": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"subnet_id": {
Type: schema.TypeString,
Computed: true,
},
"address": {
Type: schema.TypeString,
Computed: true,
},
},
},
Set: resourceLBTargetGroupTargetHash,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
},
}
}
func dataSourceYandexLBTargetGroupRead(d *schema.ResourceData, meta interface{}) error {
config := meta.(*Config)
ctx := config.Context()
err := checkOneOf(d, "target_group_id", "name")
if err != nil {
return err
}
tgID := d.Get("target_group_id").(string)
tgName, tgNameOk := d.GetOk("name")
if tgNameOk {
tgID, err = resolveObjectID(ctx, config, tgName.(string), sdkresolvers.TargetGroupResolver)
if err != nil {
return fmt.Errorf("failed to resolve data source target group by name: %v", err)
}
}
tg, err := config.sdk.LoadBalancer().TargetGroup().Get(ctx, &loadbalancer.GetTargetGroupRequest{
TargetGroupId: tgID,
})
if err != nil {
return handleNotFoundError(err, d, fmt.Sprintf("target group with ID %q", tgID))
}
createdAt, err := getTimestamp(tg.CreatedAt)
if err != nil {
return err
}
targets, err := flattenLBTargets(tg)
if err != nil {
return err
}
d.Set("target_group_id", tg.Id)
d.Set("name", tg.Name)
d.Set("description", tg.Description)
d.Set("created_at", createdAt)
d.Set("folder_id", tg.FolderId)
if err := d.Set("labels", tg.Labels); err != nil {
return err
}
if err := d.Set("target", targets); err != nil {
return err
}
d.SetId(tg.Id)
return nil
}