Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 79 additions & 0 deletions github/data_source_github_enterprise_custom_properties.go
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,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add top-level Description field


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
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

None of these should be Optional as you don't use them in the data fetching

},
}
}

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
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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
}
58 changes: 58 additions & 0 deletions github/data_source_github_enterprise_custom_properties_test.go
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)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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(
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use ConfigStateChecks

},
},
},
)
}
2 changes: 2 additions & 0 deletions github/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,7 @@ func Provider() *schema.Provider {
"github_actions_organization_workflow_permissions": resourceGithubActionsOrganizationWorkflowPermissions(),
"github_enterprise_security_analysis_settings": resourceGithubEnterpriseSecurityAnalysisSettings(),
"github_workflow_repository_permissions": resourceGithubWorkflowRepositoryPermissions(),
"github_enterprise_custom_properties": resourceGithubEnterpriseCustomProperties(),
},

DataSourcesMap: map[string]*schema.Resource{
Expand Down Expand Up @@ -294,6 +295,7 @@ func Provider() *schema.Provider {
"github_user_external_identity": dataSourceGithubUserExternalIdentity(),
"github_users": dataSourceGithubUsers(),
"github_enterprise": dataSourceGithubEnterprise(),
"github_enterprise_custom_properties": dataSourceGithubEnterpriseCustomProperties(),
"github_repository_environment_deployment_policies": dataSourceGithubRepositoryEnvironmentDeploymentPolicies(),
},
}
Expand Down
168 changes: 168 additions & 0 deletions github/resource_github_enterprise_custom_properties.go
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
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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",
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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,
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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
}
Loading