|
| 1 | +package tfe |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/url" |
| 7 | +) |
| 8 | + |
| 9 | +const OIDCConfigPathFormat = "oidc-configurations/%s" |
| 10 | + |
| 11 | +// AWSOIDCConfigurations describes all the AWS OIDC configuration related methods that the HCP Terraform API supports. |
| 12 | +// HCP Terraform API docs: |
| 13 | +// https://developer.hashicorp.com/terraform/cloud-docs/api-docs/hold-your-own-key/oidc-configurations/aws |
| 14 | +type AWSOIDCConfigurations interface { |
| 15 | + Create(ctx context.Context, organization string, options AWSOIDCConfigurationCreateOptions) (*AWSOIDCConfiguration, error) |
| 16 | + |
| 17 | + Read(ctx context.Context, oidcID string) (*AWSOIDCConfiguration, error) |
| 18 | + |
| 19 | + Update(ctx context.Context, oidcID string, options AWSOIDCConfigurationUpdateOptions) (*AWSOIDCConfiguration, error) |
| 20 | + |
| 21 | + Delete(ctx context.Context, oidcID string) error |
| 22 | +} |
| 23 | + |
| 24 | +type awsOIDCConfigurations struct { |
| 25 | + client *Client |
| 26 | +} |
| 27 | + |
| 28 | +var _ AWSOIDCConfigurations = &awsOIDCConfigurations{} |
| 29 | + |
| 30 | +type AWSOIDCConfiguration struct { |
| 31 | + ID string `jsonapi:"primary,aws-oidc-configurations"` |
| 32 | + RoleARN string `jsonapi:"attr,role-arn"` |
| 33 | + |
| 34 | + Organization *Organization `jsonapi:"relation,organization"` |
| 35 | +} |
| 36 | + |
| 37 | +type AWSOIDCConfigurationCreateOptions struct { |
| 38 | + // Type is a public field utilized by JSON:API to |
| 39 | + // set the resource type via the field tag. |
| 40 | + // It is not a user-defined value and does not need to be set. |
| 41 | + // https://jsonapi.org/format/#crud-creating |
| 42 | + Type string `jsonapi:"primary,aws-oidc-configurations"` |
| 43 | + |
| 44 | + // Attributes |
| 45 | + RoleARN string `jsonapi:"attr,role-arn"` |
| 46 | +} |
| 47 | + |
| 48 | +type AWSOIDCConfigurationUpdateOptions struct { |
| 49 | + // Type is a public field utilized by JSON:API to |
| 50 | + // set the resource type via the field tag. |
| 51 | + // It is not a user-defined value and does not need to be set. |
| 52 | + // https://jsonapi.org/format/#crud-creating |
| 53 | + Type string `jsonapi:"primary,aws-oidc-configurations"` |
| 54 | + |
| 55 | + // Attributes |
| 56 | + RoleARN string `jsonapi:"attr,role-arn"` |
| 57 | +} |
| 58 | + |
| 59 | +func (o *AWSOIDCConfigurationCreateOptions) valid() error { |
| 60 | + if o.RoleARN == "" { |
| 61 | + return ErrRequiredRoleARN |
| 62 | + } |
| 63 | + |
| 64 | + return nil |
| 65 | +} |
| 66 | + |
| 67 | +func (o *AWSOIDCConfigurationUpdateOptions) valid() error { |
| 68 | + if o.RoleARN == "" { |
| 69 | + return ErrRequiredRoleARN |
| 70 | + } |
| 71 | + |
| 72 | + return nil |
| 73 | +} |
| 74 | + |
| 75 | +func (aoc *awsOIDCConfigurations) Create(ctx context.Context, organization string, options AWSOIDCConfigurationCreateOptions) (*AWSOIDCConfiguration, error) { |
| 76 | + if !validStringID(&organization) { |
| 77 | + return nil, ErrInvalidOrg |
| 78 | + } |
| 79 | + |
| 80 | + if err := options.valid(); err != nil { |
| 81 | + return nil, err |
| 82 | + } |
| 83 | + |
| 84 | + req, err := aoc.client.NewRequest("POST", fmt.Sprintf("organizations/%s/oidc-configurations", url.PathEscape(organization)), &options) |
| 85 | + if err != nil { |
| 86 | + return nil, err |
| 87 | + } |
| 88 | + |
| 89 | + awsOIDCConfiguration := &AWSOIDCConfiguration{} |
| 90 | + err = req.Do(ctx, awsOIDCConfiguration) |
| 91 | + if err != nil { |
| 92 | + return nil, err |
| 93 | + } |
| 94 | + |
| 95 | + return awsOIDCConfiguration, nil |
| 96 | +} |
| 97 | + |
| 98 | +func (aoc *awsOIDCConfigurations) Read(ctx context.Context, oidcID string) (*AWSOIDCConfiguration, error) { |
| 99 | + req, err := aoc.client.NewRequest("GET", fmt.Sprintf(OIDCConfigPathFormat, url.PathEscape(oidcID)), nil) |
| 100 | + if err != nil { |
| 101 | + return nil, err |
| 102 | + } |
| 103 | + |
| 104 | + awsOIDCConfiguration := &AWSOIDCConfiguration{} |
| 105 | + err = req.Do(ctx, awsOIDCConfiguration) |
| 106 | + if err != nil { |
| 107 | + return nil, err |
| 108 | + } |
| 109 | + |
| 110 | + return awsOIDCConfiguration, nil |
| 111 | +} |
| 112 | + |
| 113 | +func (aoc *awsOIDCConfigurations) Update(ctx context.Context, oidcID string, options AWSOIDCConfigurationUpdateOptions) (*AWSOIDCConfiguration, error) { |
| 114 | + if !validStringID(&oidcID) { |
| 115 | + return nil, ErrInvalidOIDC |
| 116 | + } |
| 117 | + |
| 118 | + if err := options.valid(); err != nil { |
| 119 | + return nil, err |
| 120 | + } |
| 121 | + |
| 122 | + req, err := aoc.client.NewRequest("PATCH", fmt.Sprintf(OIDCConfigPathFormat, url.PathEscape(oidcID)), &options) |
| 123 | + if err != nil { |
| 124 | + return nil, err |
| 125 | + } |
| 126 | + |
| 127 | + awsOIDCConfiguration := &AWSOIDCConfiguration{} |
| 128 | + err = req.Do(ctx, awsOIDCConfiguration) |
| 129 | + if err != nil { |
| 130 | + return nil, err |
| 131 | + } |
| 132 | + |
| 133 | + return awsOIDCConfiguration, nil |
| 134 | +} |
| 135 | + |
| 136 | +func (aoc *awsOIDCConfigurations) Delete(ctx context.Context, oidcID string) error { |
| 137 | + if !validStringID(&oidcID) { |
| 138 | + return ErrInvalidOIDC |
| 139 | + } |
| 140 | + |
| 141 | + req, err := aoc.client.NewRequest("DELETE", fmt.Sprintf(OIDCConfigPathFormat, url.PathEscape(oidcID)), nil) |
| 142 | + if err != nil { |
| 143 | + return err |
| 144 | + } |
| 145 | + |
| 146 | + return req.Do(ctx, nil) |
| 147 | +} |
0 commit comments