|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "log" |
| 6 | + "strconv" |
| 7 | + |
| 8 | + "github.com/google/go-github/v66/github" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | +) |
| 11 | + |
| 12 | +func resourceGithubActionsOrganizationSecretRepository() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + Create: resourceGithubActionsOrganizationSecretRepositoryCreate, |
| 15 | + Read: resourceGithubActionsOrganizationSecretRepositoryRead, |
| 16 | + Delete: resourceGithubActionsOrganizationSecretRepositoryDelete, |
| 17 | + Importer: &schema.ResourceImporter{ |
| 18 | + StateContext: schema.ImportStatePassthroughContext, |
| 19 | + }, |
| 20 | + |
| 21 | + Schema: map[string]*schema.Schema{ |
| 22 | + "secret_name": { |
| 23 | + Type: schema.TypeString, |
| 24 | + Required: true, |
| 25 | + ForceNew: true, |
| 26 | + Description: "Name of the existing secret.", |
| 27 | + ValidateDiagFunc: validateSecretNameFunc, |
| 28 | + }, |
| 29 | + "repository_id": { |
| 30 | + Type: schema.TypeInt, |
| 31 | + Required: true, |
| 32 | + ForceNew: true, |
| 33 | + Description: "The repository ID that can access the organization secret.", |
| 34 | + }, |
| 35 | + }, |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +func resourceGithubActionsOrganizationSecretRepositoryCreate(d *schema.ResourceData, meta interface{}) error { |
| 40 | + client := meta.(*Owner).v3client |
| 41 | + owner := meta.(*Owner).name |
| 42 | + ctx := context.Background() |
| 43 | + |
| 44 | + err := checkOrganization(meta) |
| 45 | + if err != nil { |
| 46 | + return err |
| 47 | + } |
| 48 | + |
| 49 | + repositoryID := d.Get("repository_id").(int) |
| 50 | + secretName := d.Get("secret_name").(string) |
| 51 | + |
| 52 | + repoIDInt64 := int64(repositoryID) |
| 53 | + repository := &github.Repository{ |
| 54 | + ID: &repoIDInt64, |
| 55 | + } |
| 56 | + |
| 57 | + _, err = client.Actions.AddSelectedRepoToOrgSecret(ctx, owner, secretName, repository) |
| 58 | + |
| 59 | + if err != nil { |
| 60 | + return err |
| 61 | + } |
| 62 | + |
| 63 | + d.SetId(buildTwoPartID(secretName, strconv.Itoa(repositoryID))) |
| 64 | + return resourceGithubActionsOrganizationSecretRepositoryRead(d, meta) |
| 65 | +} |
| 66 | + |
| 67 | +func resourceGithubActionsOrganizationSecretRepositoryRead(d *schema.ResourceData, meta interface{}) error { |
| 68 | + owner := meta.(*Owner).name |
| 69 | + |
| 70 | + err := checkOrganization(meta) |
| 71 | + if err != nil { |
| 72 | + return err |
| 73 | + } |
| 74 | + client := meta.(*Owner).v3client |
| 75 | + |
| 76 | + secretName, repositoryIDString, err := parseTwoPartID(d.Id(), "secret_name", "repository_id") |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + |
| 81 | + repositoryID, err := strconv.ParseInt(repositoryIDString, 10, 64) |
| 82 | + if err != nil { |
| 83 | + return unconvertibleIdErr(repositoryIDString, err) |
| 84 | + } |
| 85 | + |
| 86 | + ctx := context.WithValue(context.Background(), ctxId, d.Id()) |
| 87 | + |
| 88 | + opt := &github.ListOptions{ |
| 89 | + PerPage: maxPerPage, |
| 90 | + } |
| 91 | + for { |
| 92 | + repos, resp, err := client.Actions.ListSelectedReposForOrgSecret(ctx, owner, secretName, opt) |
| 93 | + if err != nil { |
| 94 | + return err |
| 95 | + } |
| 96 | + |
| 97 | + for _, repo := range repos.Repositories { |
| 98 | + if repo.GetID() == repositoryID { |
| 99 | + if err = d.Set("secret_name", secretName); err != nil { |
| 100 | + return err |
| 101 | + } |
| 102 | + if err = d.Set("repository_id", repositoryID); err != nil { |
| 103 | + return err |
| 104 | + } |
| 105 | + return nil |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + if resp.NextPage == 0 { |
| 110 | + break |
| 111 | + } |
| 112 | + opt.Page = resp.NextPage |
| 113 | + } |
| 114 | + |
| 115 | + log.Printf("[INFO] Removing secret repository association %s from state because it no longer exists in GitHub", |
| 116 | + d.Id()) |
| 117 | + d.SetId("") |
| 118 | + |
| 119 | + return nil |
| 120 | +} |
| 121 | + |
| 122 | +func resourceGithubActionsOrganizationSecretRepositoryDelete(d *schema.ResourceData, meta interface{}) error { |
| 123 | + err := checkOrganization(meta) |
| 124 | + if err != nil { |
| 125 | + return err |
| 126 | + } |
| 127 | + client := meta.(*Owner).v3client |
| 128 | + owner := meta.(*Owner).name |
| 129 | + ctx := context.WithValue(context.Background(), ctxId, d.Id()) |
| 130 | + |
| 131 | + secretName := d.Get("secret_name").(string) |
| 132 | + repositoryID := d.Get("repository_id").(int) |
| 133 | + |
| 134 | + repoIDInt64 := int64(repositoryID) |
| 135 | + repository := &github.Repository{ |
| 136 | + ID: &repoIDInt64, |
| 137 | + } |
| 138 | + _, err = client.Actions.RemoveSelectedRepoFromOrgSecret(ctx, owner, secretName, repository) |
| 139 | + if err != nil { |
| 140 | + return err |
| 141 | + } |
| 142 | + |
| 143 | + return nil |
| 144 | +} |
0 commit comments