Skip to content

Commit e75cc27

Browse files
alilezanickfloyd
andauthored
🏳️ Custom properties resource & data (#2107)
* draft: data org custom properties * Add GitHub custom properties data source and resource * Delete github/data_source_github_custom_properties.go --------- Co-authored-by: Nick Floyd <139819+nickfloyd@users.noreply.github.com>
1 parent d67b357 commit e75cc27

File tree

3 files changed

+214
-0
lines changed

3 files changed

+214
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
package github
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
8+
)
9+
10+
func dataSourceGithubOrganizationCustomProperties() *schema.Resource {
11+
return &schema.Resource{
12+
Read: dataSourceGithubOrganizationCustomPropertiesRead,
13+
14+
Schema: map[string]*schema.Schema{
15+
"property_name": {
16+
Type: schema.TypeString,
17+
Required: true,
18+
},
19+
"value_type": {
20+
Type: schema.TypeString,
21+
Optional: true,
22+
},
23+
"required": {
24+
Type: schema.TypeBool,
25+
Optional: true,
26+
},
27+
"default_value": {
28+
Type: schema.TypeString,
29+
Optional: true,
30+
Computed: true,
31+
},
32+
"description": {
33+
Type: schema.TypeString,
34+
Optional: true,
35+
Computed: true,
36+
},
37+
"allowed_values": {
38+
Type: schema.TypeList,
39+
Optional: true,
40+
Computed: true,
41+
Elem: &schema.Schema{Type: schema.TypeString},
42+
},
43+
},
44+
}
45+
}
46+
47+
func dataSourceGithubOrganizationCustomPropertiesRead(d *schema.ResourceData, meta interface{}) error {
48+
client := meta.(*Owner).v3client
49+
ctx := context.Background()
50+
orgName := meta.(*Owner).name
51+
52+
err := checkOrganization(meta)
53+
if err != nil {
54+
return err
55+
}
56+
57+
propertyAttributes, _, err := client.Organizations.GetCustomProperty(ctx, orgName, d.Get("property_name").(string))
58+
if err != nil {
59+
return fmt.Errorf("error querying GitHub custom properties %s: %s", orgName, err)
60+
}
61+
62+
d.SetId("org-custom-properties")
63+
d.Set("allowed_values", propertyAttributes.AllowedValues)
64+
d.Set("default_value", propertyAttributes.DefaultValue)
65+
d.Set("description", propertyAttributes.Description)
66+
d.Set("property_name", propertyAttributes.PropertyName)
67+
d.Set("required", propertyAttributes.Required)
68+
d.Set("value_type", propertyAttributes.ValueType)
69+
70+
return nil
71+
}

github/provider.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ func Provider() *schema.Provider {
159159
"github_membership": resourceGithubMembership(),
160160
"github_organization_block": resourceOrganizationBlock(),
161161
"github_organization_custom_role": resourceGithubOrganizationCustomRole(),
162+
"github_organization_custom_properties": resourceGithubOrganizationCustomProperties(),
162163
"github_organization_project": resourceGithubOrganizationProject(),
163164
"github_organization_security_manager": resourceGithubOrganizationSecurityManager(),
164165
"github_organization_ruleset": resourceGithubOrganizationRuleset(),
@@ -231,6 +232,7 @@ func Provider() *schema.Provider {
231232
"github_membership": dataSourceGithubMembership(),
232233
"github_organization": dataSourceGithubOrganization(),
233234
"github_organization_custom_role": dataSourceGithubOrganizationCustomRole(),
235+
"github_organization_custom_properties": dataSourceGithubOrganizationCustomProperties(),
234236
"github_organization_external_identities": dataSourceGithubOrganizationExternalIdentities(),
235237
"github_organization_ip_allow_list": dataSourceGithubOrganizationIpAllowList(),
236238
"github_organization_team_sync_groups": dataSourceGithubOrganizationTeamSyncGroups(),
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
package github
2+
3+
import (
4+
"context"
5+
6+
"github.com/google/go-github/v57/github"
7+
"github.com/hashicorp/terraform-plugin-sdk/helper/customdiff"
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
9+
)
10+
11+
func resourceGithubOrganizationCustomProperties() *schema.Resource {
12+
return &schema.Resource{
13+
Create: resourceGithubCustomPropertiesCreate,
14+
Read: resourceGithubCustomPropertiesRead,
15+
Update: resourceGithubCustomPropertiesUpdate,
16+
Delete: resourceGithubCustomPropertiesDelete,
17+
Importer: &schema.ResourceImporter{
18+
State: resourceGithubCustomPropertiesImport,
19+
},
20+
21+
CustomizeDiff: customdiff.Sequence(
22+
customdiff.ComputedIf("slug", func(d *schema.ResourceDiff, meta interface{}) bool {
23+
return d.HasChange("name")
24+
}),
25+
),
26+
27+
Schema: map[string]*schema.Schema{
28+
"property_name": {
29+
Type: schema.TypeString,
30+
Required: true,
31+
Description: "The name of the custom property",
32+
},
33+
"value_type": {
34+
Type: schema.TypeString,
35+
Optional: true,
36+
Description: "The type of the custom property",
37+
},
38+
"required": {
39+
Type: schema.TypeBool,
40+
Optional: true,
41+
Description: "Whether the custom property is required",
42+
},
43+
"default_value": {
44+
Type: schema.TypeString,
45+
Description: "The default value of the custom property",
46+
Optional: true,
47+
Computed: true,
48+
},
49+
"description": {
50+
Description: "The description of the custom property",
51+
Type: schema.TypeString,
52+
Optional: true,
53+
Computed: true,
54+
},
55+
"allowed_values": {
56+
Description: "The allowed values of the custom property",
57+
Type: schema.TypeList,
58+
Optional: true,
59+
Computed: true,
60+
Elem: &schema.Schema{Type: schema.TypeString},
61+
},
62+
},
63+
}
64+
}
65+
66+
func resourceGithubCustomPropertiesCreate(d *schema.ResourceData, meta interface{}) error {
67+
ctx := context.Background()
68+
client := meta.(*Owner).v3client
69+
ownerName := meta.(*Owner).name
70+
71+
propertyName := d.Get("property_name").(string)
72+
valueType := d.Get("value_type").(string)
73+
required := d.Get("required").(bool)
74+
defaultValue := d.Get("default_value").(string)
75+
description := d.Get("description").(string)
76+
allowedValues := d.Get("allowed_values").([]interface{})
77+
var allowedValuesString []string
78+
for _, v := range allowedValues {
79+
allowedValuesString = append(allowedValuesString, v.(string))
80+
}
81+
82+
customProperty, _, err := client.Organizations.CreateOrUpdateCustomProperty(ctx, ownerName, d.Get("property_name").(string), &github.CustomProperty{
83+
PropertyName: &propertyName,
84+
ValueType: valueType,
85+
Required: &required,
86+
DefaultValue: &defaultValue,
87+
Description: &description,
88+
AllowedValues: allowedValuesString,
89+
})
90+
if err != nil {
91+
return err
92+
}
93+
94+
d.SetId(*customProperty.PropertyName)
95+
return resourceGithubCustomPropertiesRead(d, meta)
96+
}
97+
98+
func resourceGithubCustomPropertiesRead(d *schema.ResourceData, meta interface{}) error {
99+
ctx := context.Background()
100+
client := meta.(*Owner).v3client
101+
ownerName := meta.(*Owner).name
102+
103+
customProperty, _, err := client.Organizations.GetCustomProperty(ctx, ownerName, d.Get("property_name").(string))
104+
if err != nil {
105+
return err
106+
}
107+
108+
d.SetId(*customProperty.PropertyName)
109+
d.Set("allowed_values", customProperty.AllowedValues)
110+
d.Set("default_value", customProperty.DefaultValue)
111+
d.Set("description", customProperty.Description)
112+
d.Set("property_name", customProperty.PropertyName)
113+
d.Set("required", customProperty.Required)
114+
d.Set("value_type", customProperty.ValueType)
115+
116+
return nil
117+
}
118+
119+
func resourceGithubCustomPropertiesUpdate(d *schema.ResourceData, meta interface{}) error {
120+
if err := resourceGithubCustomPropertiesCreate(d, meta); err != nil {
121+
return err
122+
}
123+
return resourceGithubTeamRead(d, meta)
124+
}
125+
126+
func resourceGithubCustomPropertiesDelete(d *schema.ResourceData, meta interface{}) error {
127+
client := meta.(*Owner).v3client
128+
ownerName := meta.(*Owner).name
129+
130+
_, err := client.Organizations.RemoveCustomProperty(context.Background(), ownerName, d.Get("property_name").(string))
131+
if err != nil {
132+
return err
133+
}
134+
135+
return nil
136+
}
137+
138+
func resourceGithubCustomPropertiesImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
139+
// WIP
140+
return []*schema.ResourceData{d}, nil
141+
}

0 commit comments

Comments
 (0)