forked from integrations/terraform-provider-github
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathresource_github_organisation_custom_properties.go
More file actions
141 lines (123 loc) · 4.17 KB
/
resource_github_organisation_custom_properties.go
File metadata and controls
141 lines (123 loc) · 4.17 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
package github
import (
"context"
"github.com/google/go-github/v57/github"
"github.com/hashicorp/terraform-plugin-sdk/helper/customdiff"
"github.com/hashicorp/terraform-plugin-sdk/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(d *schema.ResourceDiff, meta interface{}) 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},
},
},
}
}
func resourceGithubCustomPropertiesCreate(d *schema.ResourceData, meta interface{}) 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").([]interface{})
var allowedValuesString []string
for _, v := range allowedValues {
allowedValuesString = append(allowedValuesString, v.(string))
}
customProperty, _, err := client.Organizations.CreateOrUpdateCustomProperty(ctx, ownerName, d.Get("property_name").(string), &github.CustomProperty{
PropertyName: &propertyName,
ValueType: valueType,
Required: &required,
DefaultValue: &defaultValue,
Description: &description,
AllowedValues: allowedValuesString,
})
if err != nil {
return err
}
d.SetId(*customProperty.PropertyName)
return resourceGithubCustomPropertiesRead(d, meta)
}
func resourceGithubCustomPropertiesRead(d *schema.ResourceData, meta interface{}) 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)
return nil
}
func resourceGithubCustomPropertiesUpdate(d *schema.ResourceData, meta interface{}) error {
if err := resourceGithubCustomPropertiesCreate(d, meta); err != nil {
return err
}
return resourceGithubTeamRead(d, meta)
}
func resourceGithubCustomPropertiesDelete(d *schema.ResourceData, meta interface{}) 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 interface{}) ([]*schema.ResourceData, error) {
// WIP
return []*schema.ResourceData{d}, nil
}