|
| 1 | +package gitlab |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
| 7 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 8 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation" |
| 9 | + "github.com/xanzy/go-gitlab" |
| 10 | + "log" |
| 11 | + "strconv" |
| 12 | +) |
| 13 | + |
| 14 | +func resourceGitlabManagedLicense() *schema.Resource { |
| 15 | + return &schema.Resource{ |
| 16 | + Description: "This resource allows you to add rules for managing licenses on a project.\n" + |
| 17 | + "For additional information, please see the " + |
| 18 | + "[gitlab documentation](https://docs.gitlab.com/ee/user/compliance/license_compliance/).\n\n" + |
| 19 | + "~> Using this resource requires an active [gitlab ultimate](https://about.gitlab.com/pricing/)" + |
| 20 | + "subscription.", |
| 21 | + |
| 22 | + CreateContext: resourceGitlabManagedLicenseCreate, |
| 23 | + ReadContext: resourceGitlabManagedLicenseRead, |
| 24 | + UpdateContext: resourceGitlabManagedLicenseUpdate, |
| 25 | + DeleteContext: resourceGitlabManagedLicenseDelete, |
| 26 | + Importer: &schema.ResourceImporter{ |
| 27 | + StateContext: schema.ImportStatePassthroughContext, |
| 28 | + }, |
| 29 | + |
| 30 | + Schema: map[string]*schema.Schema{ |
| 31 | + "project": { |
| 32 | + Type: schema.TypeString, |
| 33 | + Required: true, |
| 34 | + ForceNew: true, |
| 35 | + Description: "The ID of the project under which the managed license will be created.", |
| 36 | + }, |
| 37 | + "name": { |
| 38 | + Type: schema.TypeString, |
| 39 | + Required: true, |
| 40 | + // GitLab's edit API doesn't allow us to edit the name, only |
| 41 | + // the approval status. |
| 42 | + ForceNew: true, |
| 43 | + Description: "The name of the managed license (I.e., 'Apache License 2.0' or 'MIT license')", |
| 44 | + }, |
| 45 | + "approval_status": { |
| 46 | + Type: schema.TypeString, |
| 47 | + Required: true, |
| 48 | + ForceNew: false, |
| 49 | + ValidateFunc: validation.StringInSlice([]string{"approved", "blacklisted"}, true), |
| 50 | + Description: "Whether the license is approved or not. Only 'approved' or 'blacklisted' allowed.", |
| 51 | + }, |
| 52 | + }, |
| 53 | + } |
| 54 | +} |
| 55 | + |
| 56 | +func resourceGitlabManagedLicenseCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 57 | + client := meta.(*gitlab.Client) |
| 58 | + project := d.Get("project").(string) |
| 59 | + |
| 60 | + options := &gitlab.AddManagedLicenseOptions{ |
| 61 | + Name: gitlab.String(d.Get("name").(string)), |
| 62 | + ApprovalStatus: stringToApprovalStatus(d.Get("approval_status").(string)), |
| 63 | + } |
| 64 | + |
| 65 | + log.Printf("[DEBUG] create gitlab Managed License on Project %s, with Name %s", project, *options.Name) |
| 66 | + |
| 67 | + addManagedLicense, _, err := client.ManagedLicenses.AddManagedLicense(project, options, gitlab.WithContext(ctx)) |
| 68 | + if err != nil { |
| 69 | + return diag.FromErr(err) |
| 70 | + } |
| 71 | + |
| 72 | + licenseId := strconv.Itoa(addManagedLicense.ID) |
| 73 | + d.SetId(buildTwoPartID(&project, &licenseId)) |
| 74 | + |
| 75 | + return resourceGitlabManagedLicenseRead(ctx, d, meta) |
| 76 | +} |
| 77 | + |
| 78 | +func resourceGitlabManagedLicenseDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 79 | + client := meta.(*gitlab.Client) |
| 80 | + project, licenseId, err := projectIdAndLicenseIdFromId(d.Id()) |
| 81 | + if err != nil { |
| 82 | + return diag.FromErr(err) |
| 83 | + } |
| 84 | + |
| 85 | + log.Printf("[DEBUG] Delete gitlab Managed License %s", d.Id()) |
| 86 | + _, err = client.ManagedLicenses.DeleteManagedLicense(project, licenseId, gitlab.WithContext(ctx)) |
| 87 | + if err != nil { |
| 88 | + return diag.FromErr(fmt.Errorf("failed to delete managed license %d for projec %s: %w", licenseId, project, err)) |
| 89 | + } |
| 90 | + |
| 91 | + return nil |
| 92 | +} |
| 93 | + |
| 94 | +func resourceGitlabManagedLicenseRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 95 | + client := meta.(*gitlab.Client) |
| 96 | + project, licenseId, err := projectIdAndLicenseIdFromId(d.Id()) |
| 97 | + if err != nil { |
| 98 | + diag.FromErr(err) |
| 99 | + } |
| 100 | + |
| 101 | + log.Printf("[DEBUG] read gitlab Managed License for project/licenseId %s/%d", project, licenseId) |
| 102 | + license, _, err := client.ManagedLicenses.GetManagedLicense(project, licenseId, gitlab.WithContext(ctx)) |
| 103 | + if err != nil { |
| 104 | + if is404(err) { |
| 105 | + log.Printf("[DEBUG] Managed License %s:%d no longer exists and is being removed from state", project, licenseId) |
| 106 | + d.SetId("") |
| 107 | + return nil |
| 108 | + } |
| 109 | + return diag.FromErr(err) |
| 110 | + } |
| 111 | + |
| 112 | + d.Set("project", project) |
| 113 | + d.Set("name", license.Name) |
| 114 | + d.Set("approval_status", license.ApprovalStatus) |
| 115 | + |
| 116 | + return nil |
| 117 | +} |
| 118 | + |
| 119 | +func resourceGitlabManagedLicenseUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
| 120 | + client := meta.(*gitlab.Client) |
| 121 | + project, licenseId, err := projectIdAndLicenseIdFromId(d.Id()) |
| 122 | + if err != nil { |
| 123 | + return diag.FromErr(err) |
| 124 | + } |
| 125 | + |
| 126 | + opts := &gitlab.EditManagedLicenceOptions{ |
| 127 | + ApprovalStatus: stringToApprovalStatus(d.Get("approval_status").(string)), |
| 128 | + } |
| 129 | + |
| 130 | + if d.HasChange("approval_status") { |
| 131 | + opts.ApprovalStatus = stringToApprovalStatus(d.Get("approval_status").(string)) |
| 132 | + } |
| 133 | + |
| 134 | + log.Printf("[DEBUG] update gitlab Managed License %s", d.Id()) |
| 135 | + _, _, err = client.ManagedLicenses.EditManagedLicense(project, licenseId, opts, gitlab.WithContext(ctx)) |
| 136 | + if err != nil { |
| 137 | + return diag.FromErr(err) |
| 138 | + } |
| 139 | + |
| 140 | + licenseIdStr := strconv.Itoa(licenseId) |
| 141 | + d.SetId(buildTwoPartID(&project, &licenseIdStr)) |
| 142 | + |
| 143 | + return resourceGitlabManagedLicenseRead(ctx, d, meta) |
| 144 | +} |
| 145 | + |
| 146 | +// Convert the incoming string into the proper constant value for passing into the API. |
| 147 | +func stringToApprovalStatus(s string) *gitlab.LicenseApprovalStatusValue { |
| 148 | + lookup := map[string]gitlab.LicenseApprovalStatusValue{ |
| 149 | + "approved": gitlab.LicenseApproved, |
| 150 | + "blacklisted": gitlab.LicenseBlacklisted, |
| 151 | + } |
| 152 | + |
| 153 | + value, ok := lookup[s] |
| 154 | + if !ok { |
| 155 | + return nil |
| 156 | + } |
| 157 | + return &value |
| 158 | +} |
| 159 | + |
| 160 | +func projectIdAndLicenseIdFromId(id string) (string, int, error) { |
| 161 | + projectId, id, err := parseTwoPartID(id) |
| 162 | + if err != nil { |
| 163 | + return "", 0, err |
| 164 | + } |
| 165 | + |
| 166 | + licenseId, err := strconv.Atoi(id) |
| 167 | + if err != nil { |
| 168 | + return "", 0, err |
| 169 | + } |
| 170 | + |
| 171 | + return projectId, licenseId, nil |
| 172 | +} |
0 commit comments