-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathteam_data.go
102 lines (89 loc) · 2.43 KB
/
team_data.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
package provider
import (
"context"
"fmt"
"github.com/firehydrant/terraform-provider-firehydrant/firehydrant"
"github.com/hashicorp/terraform-plugin-log/tflog"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceTeam() *schema.Resource {
return &schema.Resource{
ReadContext: dataFireHydrantTeam,
Schema: map[string]*schema.Schema{
// Required
"id": {
Type: schema.TypeString,
Required: true,
},
// Computed
"name": {
Type: schema.TypeString,
Computed: true,
},
"description": {
Type: schema.TypeString,
Computed: true,
},
"slug": {
Type: schema.TypeString,
Computed: true,
},
"service_ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
"owned_service_ids": {
Type: schema.TypeSet,
Computed: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}
func dataFireHydrantTeam(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
// Get the API client
firehydrantAPIClient := m.(firehydrant.Client)
// Get the team
id := d.Get("id").(string)
tflog.Debug(ctx, fmt.Sprintf("Read team: %s", id), map[string]interface{}{
"id": id,
})
teamResponse, err := firehydrantAPIClient.Teams().Get(ctx, id)
if err != nil {
return diag.Errorf("Error reading team %s: %v", id, err)
}
// Gather values from API response
attributes := map[string]interface{}{
"id": teamResponse.ID,
"name": teamResponse.Name,
"description": teamResponse.Description,
"slug": teamResponse.Slug,
}
// Collect mapped service IDs
serviceIDs := make([]string, 0)
for _, service := range teamResponse.Services {
serviceIDs = append(serviceIDs, service.ID)
}
attributes["service_ids"] = serviceIDs
// Collect mapped owned service IDs
ownedServiceIDs := make([]string, 0)
for _, service := range teamResponse.OwnedServices {
ownedServiceIDs = append(ownedServiceIDs, service.ID)
}
attributes["owned_service_ids"] = ownedServiceIDs
// Set the data source attributes to the values we got from the API
for key, value := range attributes {
if err := d.Set(key, value); err != nil {
return diag.Errorf("Error setting %s for team %s: %v", key, id, err)
}
}
// Set the team's ID in state
d.SetId(teamResponse.ID)
return diag.Diagnostics{}
}