|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + "net/http" |
| 7 | + "regexp" |
| 8 | + |
| 9 | + "github.com/google/go-github/v83/github" |
| 10 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 11 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 12 | +) |
| 13 | + |
| 14 | +func resourceGithubOrganizationNetworkConfiguration() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Create: resourceGithubOrganizationNetworkConfigurationCreate, |
| 17 | + Read: resourceGithubOrganizationNetworkConfigurationRead, |
| 18 | + Update: resourceGithubOrganizationNetworkConfigurationUpdate, |
| 19 | + Delete: resourceGithubOrganizationNetworkConfigurationDelete, |
| 20 | + Importer: &schema.ResourceImporter{ |
| 21 | + StateContext: schema.ImportStatePassthroughContext, |
| 22 | + }, |
| 23 | + |
| 24 | + Schema: map[string]*schema.Schema{ |
| 25 | + "name": { |
| 26 | + Type: schema.TypeString, |
| 27 | + Required: true, |
| 28 | + ValidateDiagFunc: validation.ToDiagFunc(validation.All( |
| 29 | + validation.StringLenBetween(1, 100), |
| 30 | + validation.StringMatch( |
| 31 | + regexp.MustCompile(`^[a-zA-Z0-9._-]+$`), |
| 32 | + "name may only contain upper and lowercase letters a-z, numbers 0-9, '.', '-', and '_'", |
| 33 | + ), |
| 34 | + )), |
| 35 | + Description: "Name of the network configuration. Must be between 1 and 100 characters and may only contain upper and lowercase letters a-z, numbers 0-9, '.', '-', and '_'.", |
| 36 | + }, |
| 37 | + "compute_service": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Optional: true, |
| 40 | + Default: "none", |
| 41 | + ValidateDiagFunc: validation.ToDiagFunc(validation.StringInSlice([]string{"none", "actions"}, false)), |
| 42 | + Description: "The hosted compute service to use for the network configuration. Can be one of: 'none', 'actions'. Defaults to 'none'.", |
| 43 | + }, |
| 44 | + "network_settings_ids": { |
| 45 | + Type: schema.TypeList, |
| 46 | + Required: true, |
| 47 | + MinItems: 1, |
| 48 | + MaxItems: 1, |
| 49 | + Elem: &schema.Schema{ |
| 50 | + Type: schema.TypeString, |
| 51 | + }, |
| 52 | + Description: "An array containing exactly one network settings ID. A network settings resource can only be associated with one network configuration at a time.", |
| 53 | + }, |
| 54 | + "created_on": { |
| 55 | + Type: schema.TypeString, |
| 56 | + Computed: true, |
| 57 | + Description: "Timestamp when the network configuration was created.", |
| 58 | + }, |
| 59 | + }, |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +func resourceGithubOrganizationNetworkConfigurationCreate(d *schema.ResourceData, meta any) error { |
| 64 | + if err := checkOrganization(meta); err != nil { |
| 65 | + return err |
| 66 | + } |
| 67 | + |
| 68 | + client := meta.(*Owner).v3client |
| 69 | + orgName := meta.(*Owner).name |
| 70 | + ctx := context.Background() |
| 71 | + |
| 72 | + configuration, _, err := client.Organizations.CreateNetworkConfiguration(ctx, orgName, github.NetworkConfigurationRequest{ |
| 73 | + Name: github.Ptr(d.Get("name").(string)), |
| 74 | + ComputeService: expandOrganizationNetworkConfigurationComputeService(d.Get("compute_service").(string)), |
| 75 | + NetworkSettingsIDs: expandOrganizationNetworkSettingsIDs(d.Get("network_settings_ids").([]any)), |
| 76 | + }) |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + |
| 81 | + d.SetId(configuration.GetID()) |
| 82 | + |
| 83 | + return resourceGithubOrganizationNetworkConfigurationRead(d, meta) |
| 84 | +} |
| 85 | + |
| 86 | +func resourceGithubOrganizationNetworkConfigurationRead(d *schema.ResourceData, meta any) error { |
| 87 | + if err := checkOrganization(meta); err != nil { |
| 88 | + return err |
| 89 | + } |
| 90 | + |
| 91 | + client := meta.(*Owner).v3client |
| 92 | + orgName := meta.(*Owner).name |
| 93 | + networkConfigurationID := d.Id() |
| 94 | + ctx := context.WithValue(context.Background(), ctxId, networkConfigurationID) |
| 95 | + |
| 96 | + configuration, resp, err := client.Organizations.GetNetworkConfiguration(ctx, orgName, networkConfigurationID) |
| 97 | + if err != nil { |
| 98 | + if ghErr, ok := err.(*github.ErrorResponse); ok && ghErr.Response.StatusCode == http.StatusNotFound { |
| 99 | + log.Printf("[WARN] Removing organization network configuration %s from state because it no longer exists in GitHub", networkConfigurationID) |
| 100 | + d.SetId("") |
| 101 | + return nil |
| 102 | + } |
| 103 | + |
| 104 | + return err |
| 105 | + } |
| 106 | + |
| 107 | + if resp != nil && resp.StatusCode == http.StatusNotModified { |
| 108 | + return nil |
| 109 | + } |
| 110 | + |
| 111 | + _ = d.Set("name", configuration.GetName()) |
| 112 | + if configuration.ComputeService != nil { |
| 113 | + _ = d.Set("compute_service", string(*configuration.ComputeService)) |
| 114 | + } |
| 115 | + _ = d.Set("network_settings_ids", configuration.NetworkSettingsIDs) |
| 116 | + if configuration.CreatedOn != nil { |
| 117 | + _ = d.Set("created_on", configuration.CreatedOn.Format("2006-01-02T15:04:05Z07:00")) |
| 118 | + } |
| 119 | + |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +func resourceGithubOrganizationNetworkConfigurationUpdate(d *schema.ResourceData, meta any) error { |
| 124 | + if err := checkOrganization(meta); err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + |
| 128 | + client := meta.(*Owner).v3client |
| 129 | + orgName := meta.(*Owner).name |
| 130 | + networkConfigurationID := d.Id() |
| 131 | + ctx := context.WithValue(context.Background(), ctxId, networkConfigurationID) |
| 132 | + |
| 133 | + _, _, err := client.Organizations.UpdateNetworkConfiguration(ctx, orgName, networkConfigurationID, github.NetworkConfigurationRequest{ |
| 134 | + Name: github.Ptr(d.Get("name").(string)), |
| 135 | + ComputeService: expandOrganizationNetworkConfigurationComputeService(d.Get("compute_service").(string)), |
| 136 | + NetworkSettingsIDs: expandOrganizationNetworkSettingsIDs(d.Get("network_settings_ids").([]any)), |
| 137 | + }) |
| 138 | + if err != nil { |
| 139 | + return err |
| 140 | + } |
| 141 | + |
| 142 | + return resourceGithubOrganizationNetworkConfigurationRead(d, meta) |
| 143 | +} |
| 144 | + |
| 145 | +func resourceGithubOrganizationNetworkConfigurationDelete(d *schema.ResourceData, meta any) error { |
| 146 | + if err := checkOrganization(meta); err != nil { |
| 147 | + return err |
| 148 | + } |
| 149 | + |
| 150 | + client := meta.(*Owner).v3client |
| 151 | + orgName := meta.(*Owner).name |
| 152 | + ctx := context.Background() |
| 153 | + |
| 154 | + _, err := client.Organizations.DeleteNetworkConfigurations(ctx, orgName, d.Id()) |
| 155 | + if err != nil { |
| 156 | + if ghErr, ok := err.(*github.ErrorResponse); ok && ghErr.Response.StatusCode == http.StatusNotFound { |
| 157 | + return nil |
| 158 | + } |
| 159 | + |
| 160 | + return err |
| 161 | + } |
| 162 | + |
| 163 | + return nil |
| 164 | +} |
| 165 | + |
| 166 | +func expandOrganizationNetworkConfigurationComputeService(computeService string) *github.ComputeService { |
| 167 | + service := github.ComputeService(computeService) |
| 168 | + return &service |
| 169 | +} |
| 170 | + |
| 171 | +func expandOrganizationNetworkSettingsIDs(networkSettingsIDs []any) []string { |
| 172 | + ids := make([]string, 0, len(networkSettingsIDs)) |
| 173 | + for _, networkSettingsID := range networkSettingsIDs { |
| 174 | + ids = append(ids, networkSettingsID.(string)) |
| 175 | + } |
| 176 | + |
| 177 | + return ids |
| 178 | +} |
0 commit comments