|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + |
| 6 | + "github.com/google/go-github/v57/github" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/helper/customdiff" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 9 | +) |
| 10 | + |
| 11 | +func resourceGithubOrganizationCustomProperties() *schema.Resource { |
| 12 | + return &schema.Resource{ |
| 13 | + Create: resourceGithubCustomPropertiesCreate, |
| 14 | + Read: resourceGithubCustomPropertiesRead, |
| 15 | + Update: resourceGithubCustomPropertiesUpdate, |
| 16 | + Delete: resourceGithubCustomPropertiesDelete, |
| 17 | + Importer: &schema.ResourceImporter{ |
| 18 | + State: resourceGithubCustomPropertiesImport, |
| 19 | + }, |
| 20 | + |
| 21 | + CustomizeDiff: customdiff.Sequence( |
| 22 | + customdiff.ComputedIf("slug", func(d *schema.ResourceDiff, meta interface{}) bool { |
| 23 | + return d.HasChange("name") |
| 24 | + }), |
| 25 | + ), |
| 26 | + |
| 27 | + Schema: map[string]*schema.Schema{ |
| 28 | + "property_name": { |
| 29 | + Type: schema.TypeString, |
| 30 | + Required: true, |
| 31 | + Description: "The name of the custom property", |
| 32 | + }, |
| 33 | + "value_type": { |
| 34 | + Type: schema.TypeString, |
| 35 | + Optional: true, |
| 36 | + Description: "The type of the custom property", |
| 37 | + }, |
| 38 | + "required": { |
| 39 | + Type: schema.TypeBool, |
| 40 | + Optional: true, |
| 41 | + Description: "Whether the custom property is required", |
| 42 | + }, |
| 43 | + "default_value": { |
| 44 | + Type: schema.TypeString, |
| 45 | + Description: "The default value of the custom property", |
| 46 | + Optional: true, |
| 47 | + Computed: true, |
| 48 | + }, |
| 49 | + "description": { |
| 50 | + Description: "The description of the custom property", |
| 51 | + Type: schema.TypeString, |
| 52 | + Optional: true, |
| 53 | + Computed: true, |
| 54 | + }, |
| 55 | + "allowed_values": { |
| 56 | + Description: "The allowed values of the custom property", |
| 57 | + Type: schema.TypeList, |
| 58 | + Optional: true, |
| 59 | + Computed: true, |
| 60 | + Elem: &schema.Schema{Type: schema.TypeString}, |
| 61 | + }, |
| 62 | + }, |
| 63 | + } |
| 64 | +} |
| 65 | + |
| 66 | +func resourceGithubCustomPropertiesCreate(d *schema.ResourceData, meta interface{}) error { |
| 67 | + ctx := context.Background() |
| 68 | + client := meta.(*Owner).v3client |
| 69 | + ownerName := meta.(*Owner).name |
| 70 | + |
| 71 | + propertyName := d.Get("property_name").(string) |
| 72 | + valueType := d.Get("value_type").(string) |
| 73 | + required := d.Get("required").(bool) |
| 74 | + defaultValue := d.Get("default_value").(string) |
| 75 | + description := d.Get("description").(string) |
| 76 | + allowedValues := d.Get("allowed_values").([]interface{}) |
| 77 | + var allowedValuesString []string |
| 78 | + for _, v := range allowedValues { |
| 79 | + allowedValuesString = append(allowedValuesString, v.(string)) |
| 80 | + } |
| 81 | + |
| 82 | + customProperty, _, err := client.Organizations.CreateOrUpdateCustomProperty(ctx, ownerName, d.Get("property_name").(string), &github.CustomProperty{ |
| 83 | + PropertyName: &propertyName, |
| 84 | + ValueType: valueType, |
| 85 | + Required: &required, |
| 86 | + DefaultValue: &defaultValue, |
| 87 | + Description: &description, |
| 88 | + AllowedValues: allowedValuesString, |
| 89 | + }) |
| 90 | + if err != nil { |
| 91 | + return err |
| 92 | + } |
| 93 | + |
| 94 | + d.SetId(*customProperty.PropertyName) |
| 95 | + return resourceGithubCustomPropertiesRead(d, meta) |
| 96 | +} |
| 97 | + |
| 98 | +func resourceGithubCustomPropertiesRead(d *schema.ResourceData, meta interface{}) error { |
| 99 | + ctx := context.Background() |
| 100 | + client := meta.(*Owner).v3client |
| 101 | + ownerName := meta.(*Owner).name |
| 102 | + |
| 103 | + customProperty, _, err := client.Organizations.GetCustomProperty(ctx, ownerName, d.Get("property_name").(string)) |
| 104 | + if err != nil { |
| 105 | + return err |
| 106 | + } |
| 107 | + |
| 108 | + d.SetId(*customProperty.PropertyName) |
| 109 | + d.Set("allowed_values", customProperty.AllowedValues) |
| 110 | + d.Set("default_value", customProperty.DefaultValue) |
| 111 | + d.Set("description", customProperty.Description) |
| 112 | + d.Set("property_name", customProperty.PropertyName) |
| 113 | + d.Set("required", customProperty.Required) |
| 114 | + d.Set("value_type", customProperty.ValueType) |
| 115 | + |
| 116 | + return nil |
| 117 | +} |
| 118 | + |
| 119 | +func resourceGithubCustomPropertiesUpdate(d *schema.ResourceData, meta interface{}) error { |
| 120 | + if err := resourceGithubCustomPropertiesCreate(d, meta); err != nil { |
| 121 | + return err |
| 122 | + } |
| 123 | + return resourceGithubTeamRead(d, meta) |
| 124 | +} |
| 125 | + |
| 126 | +func resourceGithubCustomPropertiesDelete(d *schema.ResourceData, meta interface{}) error { |
| 127 | + client := meta.(*Owner).v3client |
| 128 | + ownerName := meta.(*Owner).name |
| 129 | + |
| 130 | + _, err := client.Organizations.RemoveCustomProperty(context.Background(), ownerName, d.Get("property_name").(string)) |
| 131 | + if err != nil { |
| 132 | + return err |
| 133 | + } |
| 134 | + |
| 135 | + return nil |
| 136 | +} |
| 137 | + |
| 138 | +func resourceGithubCustomPropertiesImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { |
| 139 | + // WIP |
| 140 | + return []*schema.ResourceData{d}, nil |
| 141 | +} |
0 commit comments