|
| 1 | +// generated file |
| 2 | +package akeyless |
| 3 | + |
| 4 | +import ( |
| 5 | + "context" |
| 6 | + |
| 7 | + akeyless_api "github.com/akeylesslabs/akeyless-go/v5" |
| 8 | + "github.com/akeylesslabs/terraform-provider-akeyless/akeyless/common" |
| 9 | + "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
| 10 | +) |
| 11 | + |
| 12 | +func resourceDigicertTarget() *schema.Resource { |
| 13 | + return &schema.Resource{ |
| 14 | + Description: "DigiCert Target resource", |
| 15 | + Create: resourceDigicertTargetCreate, |
| 16 | + Read: resourceDigicertTargetRead, |
| 17 | + Update: resourceDigicertTargetUpdate, |
| 18 | + Delete: resourceDigicertTargetDelete, |
| 19 | + Importer: &schema.ResourceImporter{ |
| 20 | + State: resourceDigicertTargetImport, |
| 21 | + }, |
| 22 | + Schema: map[string]*schema.Schema{ |
| 23 | + "name": { |
| 24 | + Type: schema.TypeString, |
| 25 | + Required: true, |
| 26 | + Description: "Target name", |
| 27 | + ForceNew: true, |
| 28 | + }, |
| 29 | + "email": { |
| 30 | + Type: schema.TypeString, |
| 31 | + Required: true, |
| 32 | + Description: "Email address for ACME account registration", |
| 33 | + }, |
| 34 | + "acme_challenge": { |
| 35 | + Type: schema.TypeString, |
| 36 | + Optional: true, |
| 37 | + Description: "ACME challenge type. Options: [dns]", |
| 38 | + Default: "dns", |
| 39 | + }, |
| 40 | + "digicert_url": { |
| 41 | + Type: schema.TypeString, |
| 42 | + Optional: true, |
| 43 | + Description: "DigiCert ACME endpoint selector. Options: [us-production/eu-production/us-demo/eu-demo]", |
| 44 | + Default: "us-production", |
| 45 | + }, |
| 46 | + "dns_target_creds": { |
| 47 | + Type: schema.TypeString, |
| 48 | + Optional: true, |
| 49 | + Description: "Name of existing cloud target for DNS credentials. Required when challenge type is dns. Supported providers: AWS, Azure, GCP", |
| 50 | + }, |
| 51 | + "eab_hmac_key": { |
| 52 | + Type: schema.TypeString, |
| 53 | + Optional: true, |
| 54 | + Sensitive: true, |
| 55 | + Description: "External Account Binding HMAC key (required for ACME account bootstrap on create)", |
| 56 | + }, |
| 57 | + "eab_key_id": { |
| 58 | + Type: schema.TypeString, |
| 59 | + Optional: true, |
| 60 | + Description: "External Account Binding key identifier (required for ACME account bootstrap on create)", |
| 61 | + }, |
| 62 | + "gcp_project": { |
| 63 | + Type: schema.TypeString, |
| 64 | + Optional: true, |
| 65 | + Description: "GCP Cloud DNS project ID. Optional and can be derived from service account", |
| 66 | + }, |
| 67 | + "hosted_zone": { |
| 68 | + Type: schema.TypeString, |
| 69 | + Optional: true, |
| 70 | + Description: "AWS Route53 hosted zone ID. Required when DNS credentials target is AWS", |
| 71 | + }, |
| 72 | + "resource_group": { |
| 73 | + Type: schema.TypeString, |
| 74 | + Optional: true, |
| 75 | + Description: "Azure resource group name. Required when DNS credentials target is Azure", |
| 76 | + }, |
| 77 | + "timeout": { |
| 78 | + Type: schema.TypeString, |
| 79 | + Optional: true, |
| 80 | + DiffSuppressFunc: common.DiffSuppressDuration, |
| 81 | + Description: "Timeout for challenge validation", |
| 82 | + Default: "5m", |
| 83 | + }, |
| 84 | + "description": { |
| 85 | + Type: schema.TypeString, |
| 86 | + Optional: true, |
| 87 | + Description: "Description of the object", |
| 88 | + }, |
| 89 | + "key": { |
| 90 | + Type: schema.TypeString, |
| 91 | + Optional: true, |
| 92 | + Computed: true, |
| 93 | + Description: "The name of a key that used to encrypt the target secret value (if empty, the account default protectionKey key will be used)", |
| 94 | + }, |
| 95 | + "max_versions": { |
| 96 | + Type: schema.TypeString, |
| 97 | + Optional: true, |
| 98 | + Description: "Set the maximum number of versions, limited by the account settings defaults.", |
| 99 | + }, |
| 100 | + "keep_prev_version": { |
| 101 | + Type: schema.TypeString, |
| 102 | + Optional: true, |
| 103 | + Description: "Whether to keep previous version [true/false]. If not set, use default according to account settings", |
| 104 | + }, |
| 105 | + }, |
| 106 | + } |
| 107 | +} |
| 108 | + |
| 109 | +func resourceDigicertTargetCreate(d *schema.ResourceData, m interface{}) error { |
| 110 | + provider := m.(*providerMeta) |
| 111 | + client := *provider.client |
| 112 | + token := *provider.token |
| 113 | + |
| 114 | + ctx := context.Background() |
| 115 | + name := d.Get("name").(string) |
| 116 | + email := d.Get("email").(string) |
| 117 | + acmeChallenge := d.Get("acme_challenge").(string) |
| 118 | + digicertUrl := d.Get("digicert_url").(string) |
| 119 | + dnsTargetCreds := d.Get("dns_target_creds").(string) |
| 120 | + eabHmacKey := d.Get("eab_hmac_key").(string) |
| 121 | + eabKeyId := d.Get("eab_key_id").(string) |
| 122 | + gcpProject := d.Get("gcp_project").(string) |
| 123 | + hostedZone := d.Get("hosted_zone").(string) |
| 124 | + resourceGroup := d.Get("resource_group").(string) |
| 125 | + timeout := d.Get("timeout").(string) |
| 126 | + description := d.Get("description").(string) |
| 127 | + key := d.Get("key").(string) |
| 128 | + maxVersions := d.Get("max_versions").(string) |
| 129 | + |
| 130 | + body := akeyless_api.TargetCreateDigiCert{ |
| 131 | + Name: name, |
| 132 | + Email: email, |
| 133 | + Token: &token, |
| 134 | + } |
| 135 | + common.GetAkeylessPtr(&body.AcmeChallenge, acmeChallenge) |
| 136 | + common.GetAkeylessPtr(&body.DigicertUrl, digicertUrl) |
| 137 | + common.GetAkeylessPtr(&body.DnsTargetCreds, dnsTargetCreds) |
| 138 | + common.GetAkeylessPtr(&body.EabHmacKey, eabHmacKey) |
| 139 | + common.GetAkeylessPtr(&body.EabKeyId, eabKeyId) |
| 140 | + common.GetAkeylessPtr(&body.GcpProject, gcpProject) |
| 141 | + common.GetAkeylessPtr(&body.HostedZone, hostedZone) |
| 142 | + common.GetAkeylessPtr(&body.ResourceGroup, resourceGroup) |
| 143 | + common.GetAkeylessPtr(&body.Timeout, timeout) |
| 144 | + common.GetAkeylessPtr(&body.Description, description) |
| 145 | + common.GetAkeylessPtr(&body.Key, key) |
| 146 | + common.GetAkeylessPtr(&body.MaxVersions, maxVersions) |
| 147 | + |
| 148 | + _, resp, err := client.TargetCreateDigiCert(ctx).Body(body).Execute() |
| 149 | + if err != nil { |
| 150 | + return common.HandleError("failed to create target", resp, err) |
| 151 | + } |
| 152 | + |
| 153 | + d.SetId(name) |
| 154 | + |
| 155 | + return nil |
| 156 | +} |
| 157 | + |
| 158 | +func resourceDigicertTargetRead(d *schema.ResourceData, m interface{}) error { |
| 159 | + provider := m.(*providerMeta) |
| 160 | + client := *provider.client |
| 161 | + token := *provider.token |
| 162 | + |
| 163 | + ctx := context.Background() |
| 164 | + |
| 165 | + path := d.Id() |
| 166 | + |
| 167 | + body := akeyless_api.TargetGetDetails{ |
| 168 | + Name: path, |
| 169 | + Token: &token, |
| 170 | + } |
| 171 | + |
| 172 | + rOut, res, err := client.TargetGetDetails(ctx).Body(body).Execute() |
| 173 | + if err != nil { |
| 174 | + return common.HandleReadError(d, "failed to get target details", res, err) |
| 175 | + } |
| 176 | + |
| 177 | + if rOut.Value != nil && rOut.Value.DigicertTargetDetails != nil { |
| 178 | + details := rOut.Value.DigicertTargetDetails |
| 179 | + if details.Email != nil { |
| 180 | + err = d.Set("email", *details.Email) |
| 181 | + if err != nil { |
| 182 | + return err |
| 183 | + } |
| 184 | + } |
| 185 | + if details.ChallengeType != nil { |
| 186 | + err = d.Set("acme_challenge", *details.ChallengeType) |
| 187 | + if err != nil { |
| 188 | + return err |
| 189 | + } |
| 190 | + } |
| 191 | + if details.DigicertDirectoryType != nil { |
| 192 | + err = d.Set("digicert_url", *details.DigicertDirectoryType) |
| 193 | + if err != nil { |
| 194 | + return err |
| 195 | + } |
| 196 | + } |
| 197 | + if details.DnsTargetName != nil { |
| 198 | + err = d.Set("dns_target_creds", *details.DnsTargetName) |
| 199 | + if err != nil { |
| 200 | + return err |
| 201 | + } |
| 202 | + } |
| 203 | + if details.EabHmacKey != nil { |
| 204 | + err = d.Set("eab_hmac_key", *details.EabHmacKey) |
| 205 | + if err != nil { |
| 206 | + return err |
| 207 | + } |
| 208 | + } |
| 209 | + if details.EabKeyId != nil { |
| 210 | + err = d.Set("eab_key_id", *details.EabKeyId) |
| 211 | + if err != nil { |
| 212 | + return err |
| 213 | + } |
| 214 | + } |
| 215 | + if details.GcpProject != nil { |
| 216 | + err = d.Set("gcp_project", *details.GcpProject) |
| 217 | + if err != nil { |
| 218 | + return err |
| 219 | + } |
| 220 | + } |
| 221 | + if details.HostedZone != nil { |
| 222 | + err = d.Set("hosted_zone", *details.HostedZone) |
| 223 | + if err != nil { |
| 224 | + return err |
| 225 | + } |
| 226 | + } |
| 227 | + if details.ResourceGroup != nil { |
| 228 | + err = d.Set("resource_group", *details.ResourceGroup) |
| 229 | + if err != nil { |
| 230 | + return err |
| 231 | + } |
| 232 | + } |
| 233 | + if details.Timeout != nil { |
| 234 | + timeout := *details.Timeout |
| 235 | + duration := common.ConvertNanoSecondsIntoDurationString(timeout) |
| 236 | + err = d.Set("timeout", duration) |
| 237 | + if err != nil { |
| 238 | + return err |
| 239 | + } |
| 240 | + } |
| 241 | + } |
| 242 | + |
| 243 | + if rOut.Target != nil { |
| 244 | + if rOut.Target.Comment != nil { |
| 245 | + err = d.Set("description", *rOut.Target.Comment) |
| 246 | + if err != nil { |
| 247 | + return err |
| 248 | + } |
| 249 | + } |
| 250 | + if rOut.Target.ProtectionKeyName != nil { |
| 251 | + err = d.Set("key", *rOut.Target.ProtectionKeyName) |
| 252 | + if err != nil { |
| 253 | + return err |
| 254 | + } |
| 255 | + } |
| 256 | + } |
| 257 | + |
| 258 | + d.SetId(path) |
| 259 | + |
| 260 | + return nil |
| 261 | +} |
| 262 | + |
| 263 | +func resourceDigicertTargetUpdate(d *schema.ResourceData, m interface{}) error { |
| 264 | + provider := m.(*providerMeta) |
| 265 | + client := *provider.client |
| 266 | + token := *provider.token |
| 267 | + |
| 268 | + ctx := context.Background() |
| 269 | + name := d.Get("name").(string) |
| 270 | + email := d.Get("email").(string) |
| 271 | + acmeChallenge := d.Get("acme_challenge").(string) |
| 272 | + digicertUrl := d.Get("digicert_url").(string) |
| 273 | + dnsTargetCreds := d.Get("dns_target_creds").(string) |
| 274 | + eabHmacKey := d.Get("eab_hmac_key").(string) |
| 275 | + eabKeyId := d.Get("eab_key_id").(string) |
| 276 | + gcpProject := d.Get("gcp_project").(string) |
| 277 | + hostedZone := d.Get("hosted_zone").(string) |
| 278 | + resourceGroup := d.Get("resource_group").(string) |
| 279 | + timeout := d.Get("timeout").(string) |
| 280 | + description := d.Get("description").(string) |
| 281 | + key := d.Get("key").(string) |
| 282 | + maxVersions := d.Get("max_versions").(string) |
| 283 | + keepPrevVersion := d.Get("keep_prev_version").(string) |
| 284 | + |
| 285 | + body := akeyless_api.TargetUpdateDigiCert{ |
| 286 | + Name: name, |
| 287 | + Email: email, |
| 288 | + Token: &token, |
| 289 | + } |
| 290 | + common.GetAkeylessPtr(&body.AcmeChallenge, acmeChallenge) |
| 291 | + common.GetAkeylessPtr(&body.DigicertUrl, digicertUrl) |
| 292 | + common.GetAkeylessPtr(&body.DnsTargetCreds, dnsTargetCreds) |
| 293 | + common.GetAkeylessPtr(&body.EabHmacKey, eabHmacKey) |
| 294 | + common.GetAkeylessPtr(&body.EabKeyId, eabKeyId) |
| 295 | + common.GetAkeylessPtr(&body.GcpProject, gcpProject) |
| 296 | + common.GetAkeylessPtr(&body.HostedZone, hostedZone) |
| 297 | + common.GetAkeylessPtr(&body.ResourceGroup, resourceGroup) |
| 298 | + common.GetAkeylessPtr(&body.Timeout, timeout) |
| 299 | + common.GetAkeylessPtr(&body.Description, description) |
| 300 | + common.GetAkeylessPtr(&body.Key, key) |
| 301 | + common.GetAkeylessPtr(&body.MaxVersions, maxVersions) |
| 302 | + common.GetAkeylessPtr(&body.KeepPrevVersion, keepPrevVersion) |
| 303 | + |
| 304 | + _, resp, err := client.TargetUpdateDigiCert(ctx).Body(body).Execute() |
| 305 | + if err != nil { |
| 306 | + return common.HandleError("failed to update target", resp, err) |
| 307 | + } |
| 308 | + |
| 309 | + d.SetId(name) |
| 310 | + |
| 311 | + return nil |
| 312 | +} |
| 313 | + |
| 314 | +func resourceDigicertTargetDelete(d *schema.ResourceData, m interface{}) error { |
| 315 | + provider := m.(*providerMeta) |
| 316 | + client := *provider.client |
| 317 | + token := *provider.token |
| 318 | + |
| 319 | + path := d.Id() |
| 320 | + |
| 321 | + deleteItem := akeyless_api.TargetDelete{ |
| 322 | + Token: &token, |
| 323 | + Name: path, |
| 324 | + } |
| 325 | + |
| 326 | + ctx := context.Background() |
| 327 | + _, _, err := client.TargetDelete(ctx).Body(deleteItem).Execute() |
| 328 | + if err != nil { |
| 329 | + return err |
| 330 | + } |
| 331 | + |
| 332 | + return nil |
| 333 | +} |
| 334 | + |
| 335 | +func resourceDigicertTargetImport(d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) { |
| 336 | + |
| 337 | + id := d.Id() |
| 338 | + |
| 339 | + err := resourceDigicertTargetRead(d, m) |
| 340 | + if err != nil { |
| 341 | + return nil, err |
| 342 | + } |
| 343 | + |
| 344 | + err = d.Set("name", id) |
| 345 | + if err != nil { |
| 346 | + return nil, err |
| 347 | + } |
| 348 | + |
| 349 | + return []*schema.ResourceData{d}, nil |
| 350 | +} |
0 commit comments