|
| 1 | +package github |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "strconv" |
| 6 | + |
| 7 | + "github.com/google/go-github/v53/github" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/helper/schema" |
| 9 | +) |
| 10 | + |
| 11 | +func resourceGithubRepositoryDeploymentBranchPolicy() *schema.Resource { |
| 12 | + return &schema.Resource{ |
| 13 | + Create: resourceGithubRepositoryDeploymentBranchPolicyCreate, |
| 14 | + Read: resourceGithubRepositoryDeploymentBranchPolicyRead, |
| 15 | + Update: resourceGithubRepositoryDeploymentBranchPolicyUpdate, |
| 16 | + Delete: resourceGithubRepositoryDeploymentBranchPolicyDelete, |
| 17 | + Importer: &schema.ResourceImporter{ |
| 18 | + State: resourceGithubRepositoryDeploymentBranchPolicyImport, |
| 19 | + }, |
| 20 | + |
| 21 | + Schema: map[string]*schema.Schema{ |
| 22 | + "repository": { |
| 23 | + Type: schema.TypeString, |
| 24 | + Required: true, |
| 25 | + ForceNew: true, |
| 26 | + Description: "The GitHub repository name.", |
| 27 | + }, |
| 28 | + "environment_name": { |
| 29 | + Type: schema.TypeString, |
| 30 | + Required: true, |
| 31 | + ForceNew: true, |
| 32 | + Description: "The target environment name.", |
| 33 | + }, |
| 34 | + "name": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Required: true, |
| 37 | + Description: "The name of the branch", |
| 38 | + }, |
| 39 | + "etag": { |
| 40 | + Type: schema.TypeString, |
| 41 | + Computed: true, |
| 42 | + Description: "An etag representing the Branch object.", |
| 43 | + }, |
| 44 | + }, |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +func resourceGithubRepositoryDeploymentBranchPolicyUpdate(d *schema.ResourceData, meta interface{}) error { |
| 49 | + ctx := context.Background() |
| 50 | + client := meta.(*Owner).v3client |
| 51 | + owner := meta.(*Owner).name |
| 52 | + repoName := d.Get("repository").(string) |
| 53 | + environmentName := d.Get("environment_name").(string) |
| 54 | + name := d.Get("name").(string) |
| 55 | + |
| 56 | + id, err := strconv.Atoi(d.Id()) |
| 57 | + if err != nil { |
| 58 | + return err |
| 59 | + } |
| 60 | + |
| 61 | + _, _, err = client.Repositories.UpdateDeploymentBranchPolicy(ctx, owner, repoName, environmentName, int64(id), &github.DeploymentBranchPolicyRequest{Name: &name}) |
| 62 | + if err != nil { |
| 63 | + return err |
| 64 | + } |
| 65 | + |
| 66 | + return resourceGithubRepositoryDeploymentBranchPolicyRead(d, meta) |
| 67 | +} |
| 68 | + |
| 69 | +func resourceGithubRepositoryDeploymentBranchPolicyCreate(d *schema.ResourceData, meta interface{}) error { |
| 70 | + ctx := context.Background() |
| 71 | + if !d.IsNewResource() { |
| 72 | + ctx = context.WithValue(ctx, ctxId, d.Id()) |
| 73 | + } |
| 74 | + |
| 75 | + client := meta.(*Owner).v3client |
| 76 | + owner := meta.(*Owner).name |
| 77 | + repoName := d.Get("repository").(string) |
| 78 | + environmentName := d.Get("environment_name").(string) |
| 79 | + name := d.Get("name").(string) |
| 80 | + |
| 81 | + policy, _, err := client.Repositories.CreateDeploymentBranchPolicy(ctx, owner, repoName, environmentName, &github.DeploymentBranchPolicyRequest{Name: &name}) |
| 82 | + if err != nil { |
| 83 | + return err |
| 84 | + } |
| 85 | + |
| 86 | + d.SetId(strconv.FormatInt(*policy.ID, 10)) |
| 87 | + |
| 88 | + return resourceGithubRepositoryDeploymentBranchPolicyRead(d, meta) |
| 89 | +} |
| 90 | + |
| 91 | +func resourceGithubRepositoryDeploymentBranchPolicyRead(d *schema.ResourceData, meta interface{}) error { |
| 92 | + ctx := context.WithValue(context.Background(), ctxId, d.Id()) |
| 93 | + if !d.IsNewResource() { |
| 94 | + ctx = context.WithValue(ctx, ctxEtag, d.Get("etag").(string)) |
| 95 | + } |
| 96 | + |
| 97 | + client := meta.(*Owner).v3client |
| 98 | + owner := meta.(*Owner).name |
| 99 | + repoName := d.Get("repository").(string) |
| 100 | + environmentName := d.Get("environment_name").(string) |
| 101 | + |
| 102 | + id, err := strconv.Atoi(d.Id()) |
| 103 | + if err != nil { |
| 104 | + return err |
| 105 | + } |
| 106 | + |
| 107 | + policy, resp, err := client.Repositories.GetDeploymentBranchPolicy(ctx, owner, repoName, environmentName, int64(id)) |
| 108 | + if err != nil && resp.StatusCode == 304 { |
| 109 | + return nil |
| 110 | + } |
| 111 | + if err != nil { |
| 112 | + return err |
| 113 | + } |
| 114 | + |
| 115 | + d.Set("etag", resp.Header.Get("ETag")) |
| 116 | + d.Set("repository", repoName) |
| 117 | + d.Set("environment_name", environmentName) |
| 118 | + d.Set("name", policy.Name) |
| 119 | + |
| 120 | + return nil |
| 121 | +} |
| 122 | + |
| 123 | +func resourceGithubRepositoryDeploymentBranchPolicyDelete(d *schema.ResourceData, meta interface{}) error { |
| 124 | + ctx := context.WithValue(context.Background(), ctxId, d.Id()) |
| 125 | + |
| 126 | + client := meta.(*Owner).v3client |
| 127 | + owner := meta.(*Owner).name |
| 128 | + repoName := d.Get("repository").(string) |
| 129 | + environmentName := d.Get("environment_name").(string) |
| 130 | + |
| 131 | + id, err := strconv.Atoi(d.Id()) |
| 132 | + if err != nil { |
| 133 | + return err |
| 134 | + } |
| 135 | + |
| 136 | + _, error := client.Repositories.DeleteDeploymentBranchPolicy(ctx, owner, repoName, environmentName, int64(id)) |
| 137 | + if error != nil { |
| 138 | + return error |
| 139 | + } |
| 140 | + return nil |
| 141 | +} |
| 142 | + |
| 143 | +func resourceGithubRepositoryDeploymentBranchPolicyImport(d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) { |
| 144 | + repoName, environmentName, id, err := parseThreePartID(d.Id(), "repository", "environment_name", "id") |
| 145 | + if err != nil { |
| 146 | + return nil, err |
| 147 | + } |
| 148 | + |
| 149 | + d.SetId(id) |
| 150 | + d.Set("repository", repoName) |
| 151 | + d.Set("environment_name", environmentName) |
| 152 | + |
| 153 | + err = resourceGithubBranchRead(d, meta) |
| 154 | + if err != nil { |
| 155 | + return nil, err |
| 156 | + } |
| 157 | + |
| 158 | + return []*schema.ResourceData{d}, nil |
| 159 | +} |
0 commit comments