|
| 1 | +package encryptionatrest_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "testing" |
| 6 | + |
| 7 | + "github.com/hashicorp/terraform-plugin-framework/types" |
| 8 | + "github.com/mongodb/terraform-provider-mongodbatlas/internal/service/encryptionatrest" |
| 9 | + "github.com/stretchr/testify/assert" |
| 10 | + "go.mongodb.org/atlas-sdk/v20231115002/admin" |
| 11 | +) |
| 12 | + |
| 13 | +var ( |
| 14 | + projectID = "projectID" |
| 15 | + enabled = true |
| 16 | + customerMasterKeyID = "CustomerMasterKeyID" |
| 17 | + region = "Region" |
| 18 | + accessKeyID = "AccessKeyID" |
| 19 | + secretAccessKey = "SecretAccessKey" |
| 20 | + roleID = "RoleID" |
| 21 | + clientID = "clientID" |
| 22 | + azureEnvironment = "AzureEnvironment" |
| 23 | + subscriptionID = "SubscriptionID" |
| 24 | + resourceGroupName = "ResourceGroupName" |
| 25 | + keyVaultName = "KeyVaultName" |
| 26 | + keyIdentifier = "KeyIdentifier" |
| 27 | + tenantID = "TenantID" |
| 28 | + secret = "Secret" |
| 29 | + keyVersionResourceID = "KeyVersionResourceID" |
| 30 | + serviceAccountKey = "ServiceAccountKey" |
| 31 | + AWSKMSConfiguration = &admin.AWSKMSConfiguration{ |
| 32 | + Enabled: &enabled, |
| 33 | + CustomerMasterKeyID: &customerMasterKeyID, |
| 34 | + Region: ®ion, |
| 35 | + AccessKeyID: &accessKeyID, |
| 36 | + SecretAccessKey: &secretAccessKey, |
| 37 | + RoleId: &roleID, |
| 38 | + } |
| 39 | + TfAwsKmsConfigModel = encryptionatrest.TfAwsKmsConfigModel{ |
| 40 | + Enabled: types.BoolValue(enabled), |
| 41 | + CustomerMasterKeyID: types.StringValue(customerMasterKeyID), |
| 42 | + Region: types.StringValue(region), |
| 43 | + AccessKeyID: types.StringValue(accessKeyID), |
| 44 | + SecretAccessKey: types.StringValue(secretAccessKey), |
| 45 | + RoleID: types.StringValue(roleID), |
| 46 | + } |
| 47 | + AzureKeyVault = &admin.AzureKeyVault{ |
| 48 | + Enabled: &enabled, |
| 49 | + ClientID: &clientID, |
| 50 | + AzureEnvironment: &azureEnvironment, |
| 51 | + SubscriptionID: &subscriptionID, |
| 52 | + ResourceGroupName: &resourceGroupName, |
| 53 | + KeyVaultName: &keyVaultName, |
| 54 | + KeyIdentifier: &keyIdentifier, |
| 55 | + TenantID: &tenantID, |
| 56 | + Secret: &secret, |
| 57 | + } |
| 58 | + TfAzureKeyVaultConfigModel = encryptionatrest.TfAzureKeyVaultConfigModel{ |
| 59 | + Enabled: types.BoolValue(enabled), |
| 60 | + ClientID: types.StringValue(clientID), |
| 61 | + AzureEnvironment: types.StringValue(azureEnvironment), |
| 62 | + SubscriptionID: types.StringValue(subscriptionID), |
| 63 | + ResourceGroupName: types.StringValue(resourceGroupName), |
| 64 | + KeyVaultName: types.StringValue(keyVaultName), |
| 65 | + KeyIdentifier: types.StringValue(keyIdentifier), |
| 66 | + TenantID: types.StringValue(tenantID), |
| 67 | + Secret: types.StringValue(secret), |
| 68 | + } |
| 69 | + GoogleCloudKMS = &admin.GoogleCloudKMS{ |
| 70 | + Enabled: &enabled, |
| 71 | + KeyVersionResourceID: &keyVersionResourceID, |
| 72 | + ServiceAccountKey: &serviceAccountKey, |
| 73 | + } |
| 74 | + TfGcpKmsConfigModel = encryptionatrest.TfGcpKmsConfigModel{ |
| 75 | + Enabled: types.BoolValue(enabled), |
| 76 | + KeyVersionResourceID: types.StringValue(keyVersionResourceID), |
| 77 | + ServiceAccountKey: types.StringValue(serviceAccountKey), |
| 78 | + } |
| 79 | + EncryptionAtRest = &admin.EncryptionAtRest{ |
| 80 | + AwsKms: AWSKMSConfiguration, |
| 81 | + AzureKeyVault: AzureKeyVault, |
| 82 | + GoogleCloudKms: GoogleCloudKMS, |
| 83 | + } |
| 84 | +) |
| 85 | + |
| 86 | +func TestNewTfEncryptionAtRestRSModel(t *testing.T) { |
| 87 | + testCases := []struct { |
| 88 | + expectedResult *encryptionatrest.TfEncryptionAtRestRSModel |
| 89 | + sdkModel *admin.EncryptionAtRest |
| 90 | + name string |
| 91 | + }{ |
| 92 | + { |
| 93 | + name: "Success NewTFAwsKmsConfig", |
| 94 | + sdkModel: EncryptionAtRest, |
| 95 | + expectedResult: &encryptionatrest.TfEncryptionAtRestRSModel{ |
| 96 | + ID: types.StringValue(projectID), |
| 97 | + ProjectID: types.StringValue(projectID), |
| 98 | + AwsKmsConfig: []encryptionatrest.TfAwsKmsConfigModel{TfAwsKmsConfigModel}, |
| 99 | + AzureKeyVaultConfig: []encryptionatrest.TfAzureKeyVaultConfigModel{TfAzureKeyVaultConfigModel}, |
| 100 | + GoogleCloudKmsConfig: []encryptionatrest.TfGcpKmsConfigModel{TfGcpKmsConfigModel}, |
| 101 | + }, |
| 102 | + }, |
| 103 | + } |
| 104 | + |
| 105 | + for _, tc := range testCases { |
| 106 | + t.Run(tc.name, func(t *testing.T) { |
| 107 | + resultModel := encryptionatrest.NewTfEncryptionAtRestRSModel(context.Background(), projectID, tc.sdkModel) |
| 108 | + assert.Equal(t, tc.expectedResult, resultModel) |
| 109 | + }) |
| 110 | + } |
| 111 | +} |
| 112 | + |
| 113 | +func TestNewTFAwsKmsConfig(t *testing.T) { |
| 114 | + testCases := []struct { |
| 115 | + name string |
| 116 | + sdkModel *admin.AWSKMSConfiguration |
| 117 | + expectedResult []encryptionatrest.TfAwsKmsConfigModel |
| 118 | + }{ |
| 119 | + { |
| 120 | + name: "Success NewTFAwsKmsConfig", |
| 121 | + sdkModel: AWSKMSConfiguration, |
| 122 | + expectedResult: []encryptionatrest.TfAwsKmsConfigModel{ |
| 123 | + TfAwsKmsConfigModel, |
| 124 | + }, |
| 125 | + }, |
| 126 | + { |
| 127 | + name: "Empty sdkModel", |
| 128 | + sdkModel: nil, |
| 129 | + expectedResult: []encryptionatrest.TfAwsKmsConfigModel{}, |
| 130 | + }, |
| 131 | + } |
| 132 | + |
| 133 | + for _, tc := range testCases { |
| 134 | + t.Run(tc.name, func(t *testing.T) { |
| 135 | + resultModel := encryptionatrest.NewTFAwsKmsConfig(context.Background(), tc.sdkModel) |
| 136 | + assert.Equal(t, tc.expectedResult, resultModel) |
| 137 | + }) |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +func TestNewTFAzureKeyVaultConfig(t *testing.T) { |
| 142 | + testCases := []struct { |
| 143 | + name string |
| 144 | + sdkModel *admin.AzureKeyVault |
| 145 | + expectedResult []encryptionatrest.TfAzureKeyVaultConfigModel |
| 146 | + }{ |
| 147 | + { |
| 148 | + name: "Success NewTFAwsKmsConfig", |
| 149 | + sdkModel: AzureKeyVault, |
| 150 | + expectedResult: []encryptionatrest.TfAzureKeyVaultConfigModel{ |
| 151 | + TfAzureKeyVaultConfigModel, |
| 152 | + }, |
| 153 | + }, |
| 154 | + { |
| 155 | + name: "Empty sdkModel", |
| 156 | + sdkModel: nil, |
| 157 | + expectedResult: []encryptionatrest.TfAzureKeyVaultConfigModel{}, |
| 158 | + }, |
| 159 | + } |
| 160 | + |
| 161 | + for _, tc := range testCases { |
| 162 | + t.Run(tc.name, func(t *testing.T) { |
| 163 | + resultModel := encryptionatrest.NewTFAzureKeyVaultConfig(context.Background(), tc.sdkModel) |
| 164 | + assert.Equal(t, tc.expectedResult, resultModel) |
| 165 | + }) |
| 166 | + } |
| 167 | +} |
| 168 | + |
| 169 | +func TestNewTFGcpKmsConfig(t *testing.T) { |
| 170 | + testCases := []struct { |
| 171 | + name string |
| 172 | + sdkModel *admin.GoogleCloudKMS |
| 173 | + expectedResult []encryptionatrest.TfGcpKmsConfigModel |
| 174 | + }{ |
| 175 | + { |
| 176 | + name: "Success NewTFGcpKmsConfig", |
| 177 | + sdkModel: GoogleCloudKMS, |
| 178 | + expectedResult: []encryptionatrest.TfGcpKmsConfigModel{ |
| 179 | + TfGcpKmsConfigModel, |
| 180 | + }, |
| 181 | + }, |
| 182 | + { |
| 183 | + name: "Empty sdkModel", |
| 184 | + sdkModel: nil, |
| 185 | + expectedResult: []encryptionatrest.TfGcpKmsConfigModel{}, |
| 186 | + }, |
| 187 | + } |
| 188 | + |
| 189 | + for _, tc := range testCases { |
| 190 | + t.Run(tc.name, func(t *testing.T) { |
| 191 | + resultModel := encryptionatrest.NewTFGcpKmsConfig(context.Background(), tc.sdkModel) |
| 192 | + assert.Equal(t, tc.expectedResult, resultModel) |
| 193 | + }) |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +func TestNewAtlasAwsKms(t *testing.T) { |
| 198 | + testCases := []struct { |
| 199 | + name string |
| 200 | + expectedResult *admin.AWSKMSConfiguration |
| 201 | + tfModel []encryptionatrest.TfAwsKmsConfigModel |
| 202 | + }{ |
| 203 | + { |
| 204 | + name: "Success NewAtlasAwsKms", |
| 205 | + tfModel: []encryptionatrest.TfAwsKmsConfigModel{TfAwsKmsConfigModel}, |
| 206 | + expectedResult: AWSKMSConfiguration, |
| 207 | + }, |
| 208 | + { |
| 209 | + name: "Empty tfModel", |
| 210 | + tfModel: nil, |
| 211 | + expectedResult: &admin.AWSKMSConfiguration{}, |
| 212 | + }, |
| 213 | + } |
| 214 | + |
| 215 | + for _, tc := range testCases { |
| 216 | + t.Run(tc.name, func(t *testing.T) { |
| 217 | + resultModel := encryptionatrest.NewAtlasAwsKms(tc.tfModel) |
| 218 | + assert.Equal(t, tc.expectedResult, resultModel) |
| 219 | + }) |
| 220 | + } |
| 221 | +} |
| 222 | + |
| 223 | +func TestNewAtlasGcpKms(t *testing.T) { |
| 224 | + testCases := []struct { |
| 225 | + name string |
| 226 | + expectedResult *admin.GoogleCloudKMS |
| 227 | + tfModel []encryptionatrest.TfGcpKmsConfigModel |
| 228 | + }{ |
| 229 | + { |
| 230 | + name: "Success NewAtlasAwsKms", |
| 231 | + tfModel: []encryptionatrest.TfGcpKmsConfigModel{TfGcpKmsConfigModel}, |
| 232 | + expectedResult: GoogleCloudKMS, |
| 233 | + }, |
| 234 | + { |
| 235 | + name: "Empty tfModel", |
| 236 | + tfModel: nil, |
| 237 | + expectedResult: &admin.GoogleCloudKMS{}, |
| 238 | + }, |
| 239 | + } |
| 240 | + |
| 241 | + for _, tc := range testCases { |
| 242 | + t.Run(tc.name, func(t *testing.T) { |
| 243 | + resultModel := encryptionatrest.NewAtlasGcpKms(tc.tfModel) |
| 244 | + assert.Equal(t, tc.expectedResult, resultModel) |
| 245 | + }) |
| 246 | + } |
| 247 | +} |
| 248 | + |
| 249 | +func TestNewAtlasAzureKeyVault(t *testing.T) { |
| 250 | + testCases := []struct { |
| 251 | + name string |
| 252 | + expectedResult *admin.AzureKeyVault |
| 253 | + tfModel []encryptionatrest.TfAzureKeyVaultConfigModel |
| 254 | + }{ |
| 255 | + { |
| 256 | + name: "Success NewAtlasAwsKms", |
| 257 | + tfModel: []encryptionatrest.TfAzureKeyVaultConfigModel{TfAzureKeyVaultConfigModel}, |
| 258 | + expectedResult: AzureKeyVault, |
| 259 | + }, |
| 260 | + { |
| 261 | + name: "Empty tfModel", |
| 262 | + tfModel: nil, |
| 263 | + expectedResult: &admin.AzureKeyVault{}, |
| 264 | + }, |
| 265 | + } |
| 266 | + |
| 267 | + for _, tc := range testCases { |
| 268 | + t.Run(tc.name, func(t *testing.T) { |
| 269 | + resultModel := encryptionatrest.NewAtlasAzureKeyVault(tc.tfModel) |
| 270 | + assert.Equal(t, tc.expectedResult, resultModel) |
| 271 | + }) |
| 272 | + } |
| 273 | +} |
0 commit comments