|
| 1 | +package kms |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "net/http" |
| 7 | + |
| 8 | + "github.com/hashicorp/terraform-plugin-framework-validators/stringvalidator" |
| 9 | + "github.com/hashicorp/terraform-plugin-framework/datasource" |
| 10 | + "github.com/hashicorp/terraform-plugin-framework/datasource/schema" |
| 11 | + "github.com/hashicorp/terraform-plugin-framework/schema/validator" |
| 12 | + "github.com/hashicorp/terraform-plugin-log/tflog" |
| 13 | + sdkUtils "github.com/stackitcloud/stackit-sdk-go/core/utils" |
| 14 | + "github.com/stackitcloud/stackit-sdk-go/services/kms" |
| 15 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/conversion" |
| 16 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/core" |
| 17 | + kmsUtils "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/services/kms/utils" |
| 18 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/utils" |
| 19 | + "github.com/stackitcloud/terraform-provider-stackit/stackit/internal/validate" |
| 20 | +) |
| 21 | + |
| 22 | +var ( |
| 23 | + _ datasource.DataSource = &keyDataSource{} |
| 24 | +) |
| 25 | + |
| 26 | +func NewKeyDataSource() datasource.DataSource { |
| 27 | + return &keyDataSource{} |
| 28 | +} |
| 29 | + |
| 30 | +type keyDataSource struct { |
| 31 | + client *kms.APIClient |
| 32 | + providerData core.ProviderData |
| 33 | +} |
| 34 | + |
| 35 | +func (k *keyDataSource) Metadata(_ context.Context, req datasource.MetadataRequest, resp *datasource.MetadataResponse) { |
| 36 | + resp.TypeName = req.ProviderTypeName + "_kms_key" |
| 37 | +} |
| 38 | + |
| 39 | +func (k *keyDataSource) Configure(ctx context.Context, req datasource.ConfigureRequest, resp *datasource.ConfigureResponse) { |
| 40 | + var ok bool |
| 41 | + k.providerData, ok = conversion.ParseProviderData(ctx, req.ProviderData, &resp.Diagnostics) |
| 42 | + if !ok { |
| 43 | + return |
| 44 | + } |
| 45 | + |
| 46 | + k.client = kmsUtils.ConfigureClient(ctx, &k.providerData, &resp.Diagnostics) |
| 47 | + if resp.Diagnostics.HasError() { |
| 48 | + return |
| 49 | + } |
| 50 | + |
| 51 | + tflog.Info(ctx, "KMS client configured") |
| 52 | +} |
| 53 | + |
| 54 | +func (k *keyDataSource) Schema(_ context.Context, _ datasource.SchemaRequest, resp *datasource.SchemaResponse) { |
| 55 | + resp.Schema = schema.Schema{ |
| 56 | + Description: fmt.Sprintf("KMS Key datasource schema. %s", core.DatasourceRegionFallbackDocstring), |
| 57 | + Attributes: map[string]schema.Attribute{ |
| 58 | + "access_scope": schema.StringAttribute{ |
| 59 | + Description: fmt.Sprintf("The access scope of the key. Default is `%s`. %s", string(kms.ACCESSSCOPE_PUBLIC), utils.FormatPossibleValues(sdkUtils.EnumSliceToStringSlice(kms.AllowedAccessScopeEnumValues)...)), |
| 60 | + Computed: true, |
| 61 | + Validators: []validator.String{ |
| 62 | + stringvalidator.LengthAtLeast(1), |
| 63 | + }, |
| 64 | + }, |
| 65 | + "algorithm": schema.StringAttribute{ |
| 66 | + Description: fmt.Sprintf("The encryption algorithm that the key will use to encrypt data. %s", utils.FormatPossibleValues(sdkUtils.EnumSliceToStringSlice(kms.AllowedAlgorithmEnumValues)...)), |
| 67 | + Computed: true, |
| 68 | + Validators: []validator.String{ |
| 69 | + stringvalidator.LengthAtLeast(1), |
| 70 | + }, |
| 71 | + }, |
| 72 | + "description": schema.StringAttribute{ |
| 73 | + Description: "A user chosen description to distinguish multiple keys", |
| 74 | + Computed: true, |
| 75 | + Validators: []validator.String{ |
| 76 | + stringvalidator.LengthAtLeast(1), |
| 77 | + }, |
| 78 | + }, |
| 79 | + "display_name": schema.StringAttribute{ |
| 80 | + Description: "The display name to distinguish multiple keys", |
| 81 | + Computed: true, |
| 82 | + Validators: []validator.String{ |
| 83 | + stringvalidator.LengthAtLeast(1), |
| 84 | + }, |
| 85 | + }, |
| 86 | + "id": schema.StringAttribute{ |
| 87 | + Description: "Terraform's internal resource ID. It is structured as \"`project_id`,`region`,`keyring_id`,`key_id`\".", |
| 88 | + Computed: true, |
| 89 | + }, |
| 90 | + "import_only": schema.BoolAttribute{ |
| 91 | + Description: "States whether versions can be created or only imported.", |
| 92 | + Computed: true, |
| 93 | + }, |
| 94 | + "key_id": schema.StringAttribute{ |
| 95 | + Description: "The ID of the key", |
| 96 | + Required: true, |
| 97 | + Validators: []validator.String{ |
| 98 | + validate.UUID(), |
| 99 | + validate.NoSeparator(), |
| 100 | + }, |
| 101 | + }, |
| 102 | + "keyring_id": schema.StringAttribute{ |
| 103 | + Description: "The ID of the associated key ring", |
| 104 | + Required: true, |
| 105 | + Validators: []validator.String{ |
| 106 | + validate.UUID(), |
| 107 | + validate.NoSeparator(), |
| 108 | + }, |
| 109 | + }, |
| 110 | + "protection": schema.StringAttribute{ |
| 111 | + Description: fmt.Sprintf("The underlying system that is responsible for protecting the key material. %s", utils.FormatPossibleValues(sdkUtils.EnumSliceToStringSlice(kms.AllowedProtectionEnumValues)...)), |
| 112 | + Computed: true, |
| 113 | + Validators: []validator.String{ |
| 114 | + stringvalidator.LengthAtLeast(1), |
| 115 | + }, |
| 116 | + }, |
| 117 | + "purpose": schema.StringAttribute{ |
| 118 | + Description: fmt.Sprintf("The purpose for which the key will be used. %s", utils.FormatPossibleValues(sdkUtils.EnumSliceToStringSlice(kms.AllowedPurposeEnumValues)...)), |
| 119 | + Computed: true, |
| 120 | + Validators: []validator.String{ |
| 121 | + stringvalidator.LengthAtLeast(1), |
| 122 | + }, |
| 123 | + }, |
| 124 | + "project_id": schema.StringAttribute{ |
| 125 | + Description: "STACKIT project ID to which the key is associated.", |
| 126 | + Required: true, |
| 127 | + Validators: []validator.String{ |
| 128 | + validate.UUID(), |
| 129 | + validate.NoSeparator(), |
| 130 | + }, |
| 131 | + }, |
| 132 | + "region": schema.StringAttribute{ |
| 133 | + Optional: true, |
| 134 | + // must be computed to allow for storing the override value from the provider |
| 135 | + Computed: true, |
| 136 | + Description: "The resource region. If not defined, the provider region is used.", |
| 137 | + }, |
| 138 | + }, |
| 139 | + } |
| 140 | +} |
| 141 | + |
| 142 | +func (k *keyDataSource) Read(ctx context.Context, req datasource.ReadRequest, resp *datasource.ReadResponse) { // nolint:gocritic // function signature required by Terraform |
| 143 | + var model Model |
| 144 | + diags := req.Config.Get(ctx, &model) |
| 145 | + resp.Diagnostics.Append(diags...) |
| 146 | + if resp.Diagnostics.HasError() { |
| 147 | + return |
| 148 | + } |
| 149 | + |
| 150 | + projectId := model.ProjectId.ValueString() |
| 151 | + keyRingId := model.KeyRingId.ValueString() |
| 152 | + region := k.providerData.GetRegionWithOverride(model.Region) |
| 153 | + keyId := model.KeyId.ValueString() |
| 154 | + |
| 155 | + ctx = tflog.SetField(ctx, "keyring_id", keyRingId) |
| 156 | + ctx = tflog.SetField(ctx, "project_id", projectId) |
| 157 | + ctx = tflog.SetField(ctx, "region", region) |
| 158 | + ctx = tflog.SetField(ctx, "key_id", keyId) |
| 159 | + |
| 160 | + keyResponse, err := k.client.GetKey(ctx, projectId, region, keyRingId, keyId).Execute() |
| 161 | + if err != nil { |
| 162 | + utils.LogError( |
| 163 | + ctx, |
| 164 | + &resp.Diagnostics, |
| 165 | + err, |
| 166 | + "Reading key", |
| 167 | + fmt.Sprintf("Key with ID %q does not exist in project %q.", keyId, projectId), |
| 168 | + map[int]string{ |
| 169 | + http.StatusForbidden: fmt.Sprintf("Project with ID %q not found or forbidden access", projectId), |
| 170 | + }, |
| 171 | + ) |
| 172 | + resp.State.RemoveResource(ctx) |
| 173 | + return |
| 174 | + } |
| 175 | + |
| 176 | + err = mapFields(keyResponse, &model, region) |
| 177 | + if err != nil { |
| 178 | + core.LogAndAddError(ctx, &resp.Diagnostics, "Error reading key", fmt.Sprintf("Processing API payload: %v", err)) |
| 179 | + return |
| 180 | + } |
| 181 | + diags = resp.State.Set(ctx, model) |
| 182 | + resp.Diagnostics.Append(diags...) |
| 183 | + if resp.Diagnostics.HasError() { |
| 184 | + return |
| 185 | + } |
| 186 | + tflog.Info(ctx, "Key read") |
| 187 | +} |
0 commit comments