forked from integrations/terraform-provider-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource_github_organization_custom_properties.go
More file actions
155 lines (136 loc) · 4.88 KB
/
resource_github_organization_custom_properties.go
File metadata and controls
155 lines (136 loc) · 4.88 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
package github
import (
"context"
"github.com/google/go-github/v67/github"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceGithubOrganizationCustomProperties() *schema.Resource {
return &schema.Resource{
Create: resourceGithubCustomPropertiesCreate,
Read: resourceGithubCustomPropertiesRead,
Update: resourceGithubCustomPropertiesUpdate,
Delete: resourceGithubCustomPropertiesDelete,
Importer: &schema.ResourceImporter{
State: resourceGithubCustomPropertiesImport,
},
CustomizeDiff: customdiff.Sequence(
customdiff.ComputedIf("slug", func(_ context.Context, d *schema.ResourceDiff, meta any) bool {
return d.HasChange("name")
}),
),
Schema: map[string]*schema.Schema{
"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",
},
"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: validateValueFunc([]string{"org_actors", "org_and_repo_actors"}),
},
},
}
}
func resourceGithubCustomPropertiesCreate(d *schema.ResourceData, meta any) error {
ctx := context.Background()
client := meta.(*Owner).v3client
ownerName := meta.(*Owner).name
propertyName := d.Get("property_name").(string)
valueType := 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))
}
valuesEditableBy := d.Get("values_editable_by").(string)
customProperty := &github.CustomProperty{
PropertyName: &propertyName,
ValueType: valueType,
Required: &required,
DefaultValue: &defaultValue,
Description: &description,
AllowedValues: allowedValuesString,
ValuesEditableBy: &valuesEditableBy,
}
customProperty, _, err := client.Organizations.CreateOrUpdateCustomProperty(ctx, ownerName, d.Get("property_name").(string), customProperty)
if err != nil {
return err
}
d.SetId(*customProperty.PropertyName)
return resourceGithubCustomPropertiesRead(d, meta)
}
func resourceGithubCustomPropertiesRead(d *schema.ResourceData, meta any) error {
ctx := context.Background()
client := meta.(*Owner).v3client
ownerName := meta.(*Owner).name
customProperty, _, err := client.Organizations.GetCustomProperty(ctx, ownerName, d.Get("property_name").(string))
if err != nil {
return err
}
d.SetId(*customProperty.PropertyName)
_ = d.Set("allowed_values", customProperty.AllowedValues)
_ = d.Set("default_value", customProperty.DefaultValue)
_ = d.Set("description", customProperty.Description)
_ = d.Set("property_name", customProperty.PropertyName)
_ = d.Set("required", customProperty.Required)
_ = d.Set("value_type", customProperty.ValueType)
_ = d.Set("values_editable_by", customProperty.ValuesEditableBy)
return nil
}
func resourceGithubCustomPropertiesUpdate(d *schema.ResourceData, meta any) error {
if err := resourceGithubCustomPropertiesCreate(d, meta); err != nil {
return err
}
return resourceGithubCustomPropertiesRead(d, meta)
}
func resourceGithubCustomPropertiesDelete(d *schema.ResourceData, meta any) error {
client := meta.(*Owner).v3client
ownerName := meta.(*Owner).name
_, err := client.Organizations.RemoveCustomProperty(context.Background(), ownerName, d.Get("property_name").(string))
if err != nil {
return err
}
return nil
}
func resourceGithubCustomPropertiesImport(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
}