forked from integrations/terraform-provider-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_source_github_repository_custom_properties.go
More file actions
103 lines (83 loc) · 2.5 KB
/
data_source_github_repository_custom_properties.go
File metadata and controls
103 lines (83 loc) · 2.5 KB
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
package github
import (
"context"
"fmt"
"github.com/google/go-github/v66/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func dataSourceGithubRepositoryCustomProperties() *schema.Resource {
return &schema.Resource{
Read: dataSourceGithubOrgaRepositoryCustomProperties,
Schema: map[string]*schema.Schema{
"repository": {
Type: schema.TypeString,
Required: true,
Description: "Name of the repository which the custom properties should be on.",
},
"property": {
Type: schema.TypeSet,
Computed: true,
Description: "List of custom properties",
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"property_name": {
Type: schema.TypeString,
Computed: true,
Description: "Name of the custom property.",
},
"property_value": {
Type: schema.TypeSet,
Computed: true,
Description: "Value of the custom property.",
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
},
},
},
}
}
func dataSourceGithubOrgaRepositoryCustomProperties(d *schema.ResourceData, meta interface{}) error {
client := meta.(*Owner).v3client
ctx := context.Background()
owner := meta.(*Owner).name
repoName := d.Get("repository").(string)
allCustomProperties, _, err := client.Repositories.GetAllCustomPropertyValues(ctx, owner, repoName)
if err != nil {
return err
}
results, err := flattenRepositoryCustomProperties(allCustomProperties)
if err != nil {
return err
}
d.SetId(buildTwoPartID(owner, repoName))
d.Set("repository", repoName)
d.Set("property", results)
return nil
}
func flattenRepositoryCustomProperties(customProperties []*github.CustomPropertyValue) ([]interface{}, error) {
results := make([]interface{}, 0)
for _, prop := range customProperties {
result := make(map[string]interface{})
result["property_name"] = prop.PropertyName
propertyValue, err := parseRepositoryCustomPropertyValueToStringSlice(prop)
if err != nil {
return nil, err
}
result["property_value"] = propertyValue
results = append(results, result)
}
return results, nil
}
func parseRepositoryCustomPropertyValueToStringSlice(prop *github.CustomPropertyValue) ([]string, error) {
switch value := prop.Value.(type) {
case string:
return []string{value}, nil
case []string:
return value, nil
default:
return nil, fmt.Errorf("custom property value couldn't be parsed as a string or a list of strings: %s", value)
}
}