Skip to content

Commit b4fdf7b

Browse files
committed
feat(custom_properties): enhance resource with context support and error handling
1 parent de43fdf commit b4fdf7b

1 file changed

Lines changed: 79 additions & 44 deletions

File tree

github/resource_github_organization_custom_properties.go

Lines changed: 79 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,20 @@ import (
77
"net/http"
88

99
"github.com/google/go-github/v84/github"
10+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
1011
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
1112
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
1213
)
1314

1415
func resourceGithubOrganizationCustomProperties() *schema.Resource {
1516
return &schema.Resource{
16-
Description: "Creates and manages a custom property for a GitHub Organization.",
17-
Create: resourceGithubCustomPropertiesCreate,
18-
Read: resourceGithubCustomPropertiesRead,
19-
Update: resourceGithubCustomPropertiesUpdate,
20-
Delete: resourceGithubCustomPropertiesDelete,
17+
Description: "Creates and manages a custom property for a GitHub Organization.",
18+
CreateContext: resourceGithubCustomPropertiesCreate,
19+
ReadContext: resourceGithubCustomPropertiesRead,
20+
UpdateContext: resourceGithubCustomPropertiesUpdate,
21+
DeleteContext: resourceGithubCustomPropertiesDelete,
2122
Importer: &schema.ResourceImporter{
22-
State: resourceGithubCustomPropertiesImport,
23+
StateContext: resourceGithubCustomPropertiesImport,
2324
},
2425

2526
Schema: map[string]*schema.Schema{
@@ -71,15 +72,8 @@ func resourceGithubOrganizationCustomProperties() *schema.Resource {
7172
}
7273
}
7374

74-
func resourceGithubCustomPropertiesCreate(d *schema.ResourceData, meta any) error {
75-
if err := checkOrganization(meta); err != nil {
76-
return err
77-
}
78-
79-
ctx := context.Background()
80-
client := meta.(*Owner).v3client
81-
ownerName := meta.(*Owner).name
82-
75+
// buildCustomProperty constructs a github.CustomProperty from the resource data.
76+
func buildCustomProperty(d *schema.ResourceData) (*github.CustomProperty, error) {
8377
propertyName := d.Get("property_name").(string)
8478
valueType := github.PropertyValueType(d.Get("value_type").(string))
8579
required := d.Get("required").(bool)
@@ -104,35 +98,67 @@ func resourceGithubCustomPropertiesCreate(d *schema.ResourceData, meta any) erro
10498
if valueType == github.PropertyValueTypeSingleSelect || valueType == github.PropertyValueTypeMultiSelect {
10599
customProperty.AllowedValues = allowedValues
106100
} else {
107-
return fmt.Errorf("allowed_values can only be set for single_select or multi_select value types")
101+
return nil, fmt.Errorf("allowed_values can only be set for single_select or multi_select value types")
108102
}
109103
}
110104

111105
// Validate that allowed_values is provided for select types
112106
if (valueType == github.PropertyValueTypeSingleSelect || valueType == github.PropertyValueTypeMultiSelect) && len(customProperty.AllowedValues) == 0 {
113-
return fmt.Errorf("allowed_values is required for %s value type", valueType)
107+
return nil, fmt.Errorf("allowed_values is required for %s value type", valueType)
114108
}
115109

116110
if val, ok := d.GetOk("values_editable_by"); ok {
117111
str := val.(string)
118112
customProperty.ValuesEditableBy = &str
119113
}
120114

121-
customProperty, _, err := client.Organizations.CreateOrUpdateCustomProperty(ctx, ownerName, propertyName, customProperty)
115+
return customProperty, nil
116+
}
117+
118+
// setCustomPropertyState sets all resource data fields from a CustomProperty API response.
119+
func setCustomPropertyState(d *schema.ResourceData, customProperty *github.CustomProperty) {
120+
// TODO: Add support for other types of default values
121+
defaultValue, _ := customProperty.DefaultValueString()
122+
123+
d.SetId(*customProperty.PropertyName)
124+
_ = d.Set("allowed_values", customProperty.AllowedValues)
125+
_ = d.Set("default_value", defaultValue)
126+
_ = d.Set("description", customProperty.Description)
127+
_ = d.Set("property_name", customProperty.PropertyName)
128+
_ = d.Set("required", customProperty.Required)
129+
_ = d.Set("value_type", string(customProperty.ValueType))
130+
_ = d.Set("values_editable_by", customProperty.ValuesEditableBy)
131+
}
132+
133+
func resourceGithubCustomPropertiesCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
134+
if err := checkOrganization(meta); err != nil {
135+
return diag.FromErr(err)
136+
}
137+
138+
client := meta.(*Owner).v3client
139+
ownerName := meta.(*Owner).name
140+
141+
customProperty, err := buildCustomProperty(d)
122142
if err != nil {
123-
return fmt.Errorf("error creating organization custom property %s: %w", propertyName, err)
143+
return diag.FromErr(err)
124144
}
125145

126-
d.SetId(*customProperty.PropertyName)
127-
return resourceGithubCustomPropertiesRead(d, meta)
146+
propertyName := d.Get("property_name").(string)
147+
customProperty, _, err = client.Organizations.CreateOrUpdateCustomProperty(ctx, ownerName, propertyName, customProperty)
148+
if err != nil {
149+
return diag.FromErr(fmt.Errorf("error creating organization custom property %s: %w", propertyName, err))
150+
}
151+
152+
setCustomPropertyState(d, customProperty)
153+
154+
return nil
128155
}
129156

130-
func resourceGithubCustomPropertiesRead(d *schema.ResourceData, meta any) error {
157+
func resourceGithubCustomPropertiesRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
131158
if err := checkOrganization(meta); err != nil {
132-
return err
159+
return diag.FromErr(err)
133160
}
134161

135-
ctx := context.Background()
136162
client := meta.(*Owner).v3client
137163
ownerName := meta.(*Owner).name
138164

@@ -148,47 +174,56 @@ func resourceGithubCustomPropertiesRead(d *schema.ResourceData, meta any) error
148174
d.SetId("")
149175
return nil
150176
}
151-
return fmt.Errorf("error reading organization custom property %s: %w", propertyName, err)
177+
return diag.FromErr(fmt.Errorf("error reading organization custom property %s: %w", propertyName, err))
152178
}
153179

154-
// TODO: Add support for other types of default values
155-
defaultValue, _ := customProperty.DefaultValueString()
156-
157-
d.SetId(*customProperty.PropertyName)
158-
_ = d.Set("allowed_values", customProperty.AllowedValues)
159-
_ = d.Set("default_value", defaultValue)
160-
_ = d.Set("description", customProperty.Description)
161-
_ = d.Set("property_name", customProperty.PropertyName)
162-
_ = d.Set("required", customProperty.Required)
163-
_ = d.Set("value_type", string(customProperty.ValueType))
164-
_ = d.Set("values_editable_by", customProperty.ValuesEditableBy)
180+
setCustomPropertyState(d, customProperty)
165181

166182
return nil
167183
}
168184

169-
func resourceGithubCustomPropertiesUpdate(d *schema.ResourceData, meta any) error {
170-
// Create uses the same upsert API, and already calls Read at the end
171-
return resourceGithubCustomPropertiesCreate(d, meta)
185+
func resourceGithubCustomPropertiesUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
186+
if err := checkOrganization(meta); err != nil {
187+
return diag.FromErr(err)
188+
}
189+
190+
client := meta.(*Owner).v3client
191+
ownerName := meta.(*Owner).name
192+
193+
customProperty, err := buildCustomProperty(d)
194+
if err != nil {
195+
return diag.FromErr(err)
196+
}
197+
198+
propertyName := d.Get("property_name").(string)
199+
customProperty, _, err = client.Organizations.CreateOrUpdateCustomProperty(ctx, ownerName, propertyName, customProperty)
200+
if err != nil {
201+
return diag.FromErr(fmt.Errorf("error updating organization custom property %s: %w", propertyName, err))
202+
}
203+
204+
setCustomPropertyState(d, customProperty)
205+
206+
return nil
172207
}
173208

174-
func resourceGithubCustomPropertiesDelete(d *schema.ResourceData, meta any) error {
209+
func resourceGithubCustomPropertiesDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
175210
if err := checkOrganization(meta); err != nil {
176-
return err
211+
return diag.FromErr(err)
177212
}
178213

179214
client := meta.(*Owner).v3client
180215
ownerName := meta.(*Owner).name
181216
propertyName := d.Get("property_name").(string)
182217

183-
_, err := client.Organizations.RemoveCustomProperty(context.Background(), ownerName, propertyName)
218+
_, err := client.Organizations.RemoveCustomProperty(ctx, ownerName, propertyName)
184219
if err != nil {
185-
return fmt.Errorf("error deleting organization custom property %s: %w", propertyName, err)
220+
return diag.FromErr(fmt.Errorf("error deleting organization custom property %s: %w", propertyName, err))
186221
}
187222

188223
return nil
189224
}
190225

191-
func resourceGithubCustomPropertiesImport(d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
226+
func resourceGithubCustomPropertiesImport(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
192227
if err := d.Set("property_name", d.Id()); err != nil {
193228
return nil, err
194229
}

0 commit comments

Comments
 (0)