|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | +) |
| 9 | + |
| 10 | +func dataSourceGithubEnterpriseCustomProperty() *schema.Resource { |
| 11 | + return &schema.Resource{ |
| 12 | + ReadContext: dataSourceGithubEnterpriseCustomPropertyRead, |
| 13 | + |
| 14 | + Schema: map[string]*schema.Schema{ |
| 15 | + "enterprise_slug": { |
| 16 | + Type: schema.TypeString, |
| 17 | + Required: true, |
| 18 | + Description: "The slug of the enterprise.", |
| 19 | + }, |
| 20 | + "property_name": { |
| 21 | + Type: schema.TypeString, |
| 22 | + Required: true, |
| 23 | + Description: "The name of the custom property.", |
| 24 | + }, |
| 25 | + "value_type": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Computed: true, |
| 28 | + Description: "The type of the value for the property.", |
| 29 | + }, |
| 30 | + "required": { |
| 31 | + Type: schema.TypeBool, |
| 32 | + Computed: true, |
| 33 | + Description: "Whether the custom property is required.", |
| 34 | + }, |
| 35 | + "default_value": { |
| 36 | + Type: schema.TypeString, |
| 37 | + Computed: true, |
| 38 | + Description: "The default value of the custom property.", |
| 39 | + }, |
| 40 | + "description": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Computed: true, |
| 43 | + Description: "A short description of the custom property.", |
| 44 | + }, |
| 45 | + "allowed_values": { |
| 46 | + Type: schema.TypeList, |
| 47 | + Computed: true, |
| 48 | + Description: "An ordered list of allowed values for the property.", |
| 49 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 50 | + }, |
| 51 | + "values_editable_by": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Computed: true, |
| 54 | + Description: "Who can edit the values of the property. Can be one of 'org_actors' or 'org_and_repo_actors'.", |
| 55 | + }, |
| 56 | + }, |
| 57 | + } |
| 58 | +} |
| 59 | + |
| 60 | +func dataSourceGithubEnterpriseCustomPropertyRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics { |
| 61 | + client := meta.(*Owner).v3client |
| 62 | + |
| 63 | + enterpriseSlug := d.Get("enterprise_slug").(string) |
| 64 | + propertyName := d.Get("property_name").(string) |
| 65 | + |
| 66 | + property, _, err := client.Enterprise.GetCustomProperty(ctx, enterpriseSlug, propertyName) |
| 67 | + if err != nil { |
| 68 | + return diag.Errorf("error reading enterprise custom property %s/%s: %v", enterpriseSlug, propertyName, err) |
| 69 | + } |
| 70 | + |
| 71 | + defaultValue, _ := property.DefaultValueString() |
| 72 | + |
| 73 | + d.SetId(buildTwoPartID(enterpriseSlug, propertyName)) |
| 74 | + _ = d.Set("enterprise_slug", enterpriseSlug) |
| 75 | + _ = d.Set("property_name", property.GetPropertyName()) |
| 76 | + _ = d.Set("value_type", string(property.ValueType)) |
| 77 | + _ = d.Set("required", property.GetRequired()) |
| 78 | + _ = d.Set("default_value", defaultValue) |
| 79 | + _ = d.Set("description", property.GetDescription()) |
| 80 | + _ = d.Set("allowed_values", property.AllowedValues) |
| 81 | + _ = d.Set("values_editable_by", property.GetValuesEditableBy()) |
| 82 | + |
| 83 | + return nil |
| 84 | +} |
0 commit comments