-
Notifications
You must be signed in to change notification settings - Fork 951
Add support for setting enterprise properties #3292
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
b97a81c
f54c366
9c42518
a22aad1
6579448
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "context" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-sdk/v2/diag" | ||
| "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" | ||
| ) | ||
|
|
||
| func dataSourceGithubEnterpriseCustomProperties() *schema.Resource { | ||
| return &schema.Resource{ | ||
| ReadContext: dataSourceGithubEnterpriseCustomPropertiesRead, | ||
|
|
||
| Schema: map[string]*schema.Schema{ | ||
| "enterprise_slug": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| }, | ||
| "property_name": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| }, | ||
| "value_type": { | ||
| Type: schema.TypeString, | ||
| Optional: true, | ||
| }, | ||
| "required": { | ||
| Type: schema.TypeBool, | ||
| Optional: true, | ||
| }, | ||
| "default_value": { | ||
| Type: schema.TypeString, | ||
| Optional: true, | ||
| Computed: true, | ||
| }, | ||
| "description": { | ||
| Type: schema.TypeString, | ||
| Optional: true, | ||
| Computed: true, | ||
| }, | ||
| "allowed_values": { | ||
| Type: schema.TypeList, | ||
| Optional: true, | ||
| Computed: true, | ||
| Elem: &schema.Schema{Type: schema.TypeString}, | ||
| }, | ||
| "values_editable_by": { | ||
| Type: schema.TypeString, | ||
| Optional: true, | ||
| Computed: true, | ||
| }, | ||
|
Comment on lines
+23
to
+51
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. None of these should be |
||
| }, | ||
| } | ||
| } | ||
|
|
||
| func dataSourceGithubEnterpriseCustomPropertiesRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { | ||
| client := meta.(*Owner).v3client | ||
| entSlug := d.Get("enterprise_slug").(string) | ||
|
|
||
| propertyAttributes, _, err := client.Enterprise.GetCustomProperty(ctx, entSlug, d.Get("property_name").(string)) | ||
| if err != nil { | ||
| return diag.Errorf("error querying GitHub custom properties %s: %v", entSlug, err) | ||
| } | ||
|
|
||
| // TODO: Add support for other types of default values | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please don't submit PRs with missing implementation |
||
| defaultValue, _ := propertyAttributes.DefaultValueString() | ||
|
|
||
| d.SetId("enterprise-custom-properties") | ||
| _ = d.Set("enterprise_slug", entSlug) | ||
| _ = d.Set("allowed_values", propertyAttributes.AllowedValues) | ||
| _ = d.Set("default_value", defaultValue) | ||
| _ = d.Set("description", propertyAttributes.Description) | ||
| _ = d.Set("property_name", propertyAttributes.PropertyName) | ||
| _ = d.Set("required", propertyAttributes.Required) | ||
| _ = d.Set("value_type", string(propertyAttributes.ValueType)) | ||
| _ = d.Set("values_editable_by", propertyAttributes.ValuesEditableBy) | ||
|
|
||
| return nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| package github | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
| "github.com/hashicorp/terraform-plugin-testing/helper/acctest" | ||
| ) | ||
|
|
||
| func TestAccGithubEnterpriseCustomPropertiesDataSource(t *testing.T) { | ||
| randomID := acctest.RandStringFromCharSet(5, acctest.CharSetAlpha) | ||
| propertyName := fmt.Sprintf("test-%s", randomID) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please use testResourcePrefix |
||
|
|
||
| config := fmt.Sprintf(` | ||
| resource "github_enterprise_custom_properties" "test" { | ||
| enterprise_slug = "%s" | ||
| property_name = "%s" | ||
| value_type = "string" | ||
| required = true | ||
| default_value = "terraform" | ||
| description = "A test property" | ||
| values_editable_by = "org_and_repo_actors" | ||
| } | ||
|
|
||
| data "github_enterprise_custom_properties" "test" { | ||
| enterprise_slug = "%s" | ||
| property_name = github_enterprise_custom_properties.test.property_name | ||
| } | ||
| `, | ||
| testAccConf.enterpriseSlug, | ||
| propertyName, | ||
| testAccConf.enterpriseSlug, | ||
| ) | ||
|
|
||
| check := resource.ComposeTestCheckFunc( | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't use a variable |
||
| resource.TestCheckResourceAttr("data.github_enterprise_custom_properties.test", "enterprise_slug", testAccConf.enterpriseSlug), | ||
| resource.TestCheckResourceAttr("data.github_enterprise_custom_properties.test", "property_name", propertyName), | ||
| resource.TestCheckResourceAttr("data.github_enterprise_custom_properties.test", "allowed_values.#", "0"), | ||
| resource.TestCheckResourceAttr("data.github_enterprise_custom_properties.test", "value_type", "string"), | ||
| resource.TestCheckResourceAttr("data.github_enterprise_custom_properties.test", "required", "true"), | ||
| resource.TestCheckResourceAttr("data.github_enterprise_custom_properties.test", "default_value", "terraform"), | ||
| resource.TestCheckResourceAttr("data.github_enterprise_custom_properties.test", "description", "A test property"), | ||
| resource.TestCheckResourceAttr("data.github_enterprise_custom_properties.test", "values_editable_by", "org_and_repo_actors"), | ||
| ) | ||
|
|
||
| resource.Test(t, resource.TestCase{ | ||
| PreCheck: func() { skipUnlessEnterprise(t) }, | ||
| ProviderFactories: providerFactories, | ||
| Steps: []resource.TestStep{ | ||
| { | ||
| Config: config, | ||
| Check: check, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use ConfigStateChecks |
||
| }, | ||
| }, | ||
| }, | ||
| ) | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,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, | ||
| }, | ||
|
Comment on lines
+14
to
+20
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Use Contex-aware functions |
||
|
|
||
| CustomizeDiff: customdiff.Sequence( | ||
| customdiff.ComputedIf("slug", func(_ context.Context, d *schema.ResourceDiff, meta any) bool { | ||
| return d.HasChange("name") | ||
|
Comment on lines
+23
to
+24
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Neither of these fields exist? |
||
| }), | ||
| ), | ||
|
|
||
| Schema: map[string]*schema.Schema{ | ||
| "enterprise_slug": { | ||
| Type: schema.TypeString, | ||
| Required: true, | ||
| Description: "The slug of the enterprise to which the custom property belongs", | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should have ForceNew as well |
||
| }, | ||
| "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, | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why would this be computed? I doubt the API has a default value for this field |
||
| }, | ||
| "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) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Never call any of the CRUD functions |
||
| } | ||
|
|
||
| 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) | ||
|
Comment on lines
+133
to
+139
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Don't swallow errors |
||
|
|
||
| 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 | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add 404 handling |
||
| } | ||
|
|
||
| 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 | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add top-level
Descriptionfield