-
Notifications
You must be signed in to change notification settings - Fork 958
Expand file tree
/
Copy pathresource_github_enterprise_custom_properties.go
More file actions
168 lines (147 loc) · 5.73 KB
/
resource_github_enterprise_custom_properties.go
File metadata and controls
168 lines (147 loc) · 5.73 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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
package github
import (
"context"
"github.com/google/go-github/v84/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
)
func resourceGithubEnterpriseCustomProperties() *schema.Resource {
return &schema.Resource{
Create: resourceGithubEnterpriseCustomPropertiesCreate,
Read: resourceGithubEnterpriseCustomPropertiesRead,
Update: resourceGithubEnterpriseCustomPropertiesUpdate,
Delete: resourceGithubEnterpriseCustomPropertiesDelete,
Importer: &schema.ResourceImporter{
State: resourceGithubEnterpriseCustomPropertiesImport,
},
CustomizeDiff: customdiff.Sequence(
customdiff.ComputedIf("slug", func(_ context.Context, d *schema.ResourceDiff, meta any) bool {
return d.HasChange("name")
}),
),
Schema: map[string]*schema.Schema{
"enterprise_slug": {
Type: schema.TypeString,
Required: true,
Description: "The slug of the enterprise to which the custom property belongs",
},
"property_name": {
Type: schema.TypeString,
Required: true,
Description: "The name of the custom property",
},
"value_type": {
Type: schema.TypeString,
Optional: true,
Description: "The type of the custom property",
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{string(github.PropertyValueTypeString), string(github.PropertyValueTypeSingleSelect), string(github.PropertyValueTypeMultiSelect), string(github.PropertyValueTypeTrueFalse), string(github.PropertyValueTypeURL)}, false)),
},
"required": {
Type: schema.TypeBool,
Optional: true,
Description: "Whether the custom property is required",
},
"default_value": {
Type: schema.TypeString,
Description: "The default value of the custom property",
Optional: true,
Computed: true,
},
"description": {
Description: "The description of the custom property",
Type: schema.TypeString,
Optional: true,
Computed: true,
},
"allowed_values": {
Description: "The allowed values of the custom property",
Type: schema.TypeList,
Optional: true,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"values_editable_by": {
Description: "Who can edit the values of the custom property. Can be one of 'org_actors' or 'org_and_repo_actors'. If not specified, the default is 'org_actors' (only organization owners can edit values)",
Type: schema.TypeString,
Optional: true,
Computed: true,
ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{"org_actors", "org_and_repo_actors"}, false)),
},
},
}
}
func resourceGithubEnterpriseCustomPropertiesCreate(d *schema.ResourceData, meta any) error {
ctx := context.Background()
client := meta.(*Owner).v3client
entSlug := d.Get("enterprise_slug").(string)
propertyName := d.Get("property_name").(string)
valueType := github.PropertyValueType(d.Get("value_type").(string))
required := d.Get("required").(bool)
defaultValue := d.Get("default_value").(string)
description := d.Get("description").(string)
allowedValues := d.Get("allowed_values").([]any)
var allowedValuesString []string
for _, v := range allowedValues {
allowedValuesString = append(allowedValuesString, v.(string))
}
customProperty := &github.CustomProperty{
PropertyName: &propertyName,
ValueType: valueType,
Required: &required,
DefaultValue: &defaultValue,
Description: &description,
AllowedValues: allowedValuesString,
}
if val, ok := d.GetOk("values_editable_by"); ok {
str := val.(string)
customProperty.ValuesEditableBy = &str
}
customProperty, _, err := client.Enterprise.CreateOrUpdateCustomProperty(ctx, entSlug, d.Get("property_name").(string), customProperty)
if err != nil {
return err
}
d.SetId(*customProperty.PropertyName)
return resourceGithubEnterpriseCustomPropertiesRead(d, meta)
}
func resourceGithubEnterpriseCustomPropertiesRead(d *schema.ResourceData, meta any) error {
ctx := context.Background()
client := meta.(*Owner).v3client
entSlug := d.Get("enterprise_slug").(string)
customProperty, _, err := client.Enterprise.GetCustomProperty(ctx, entSlug, d.Get("property_name").(string))
if err != nil {
return err
}
// TODO: Add support for other types of default values
defaultValue, _ := customProperty.DefaultValueString()
d.SetId(*customProperty.PropertyName)
_ = d.Set("allowed_values", customProperty.AllowedValues)
_ = d.Set("default_value", defaultValue)
_ = d.Set("description", customProperty.Description)
_ = d.Set("property_name", customProperty.PropertyName)
_ = d.Set("required", customProperty.Required)
_ = d.Set("value_type", string(customProperty.ValueType))
_ = d.Set("values_editable_by", customProperty.ValuesEditableBy)
return nil
}
func resourceGithubEnterpriseCustomPropertiesUpdate(d *schema.ResourceData, meta any) error {
if err := resourceGithubEnterpriseCustomPropertiesCreate(d, meta); err != nil {
return err
}
return resourceGithubEnterpriseCustomPropertiesRead(d, meta)
}
func resourceGithubEnterpriseCustomPropertiesDelete(d *schema.ResourceData, meta any) error {
client := meta.(*Owner).v3client
entSlug := d.Get("enterprise_slug").(string)
_, err := client.Enterprise.RemoveCustomProperty(context.Background(), entSlug, d.Get("property_name").(string))
if err != nil {
return err
}
return nil
}
func resourceGithubEnterpriseCustomPropertiesImport(d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
if err := d.Set("property_name", d.Id()); err != nil {
return nil, err
}
return []*schema.ResourceData{d}, nil
}