Skip to content

Commit

Permalink
chore: Updates online_archive resource with new SDK (#1875)
Browse files Browse the repository at this point in the history
* update SDK

* update logic for data_process_region

* rename resource functions

* tests

* change comment
  • Loading branch information
lantoli authored Jan 26, 2024
1 parent 5092677 commit f65f3fb
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 98 deletions.
2 changes: 1 addition & 1 deletion internal/config/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ var (
type MongoDBClient struct {
Atlas *matlasClient.Client
AtlasV2 *admin.APIClient
Atlas20231001002 *admin20231001002.APIClient // Needed to avoid breaking changes in federated_settings_identity_provider and online_archive resources.
Atlas20231001002 *admin20231001002.APIClient // Needed to avoid breaking changes in federated_settings_identity_provider resource.
Config *Config
}

Expand Down
10 changes: 5 additions & 5 deletions internal/service/onlinearchive/data_source_online_archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,12 @@ func schemaOnlineArchive() map[string]*schema.Schema {
}

func dataSourceMongoDBAtlasOnlineArchiveRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
conn20231001002 := meta.(*config.MongoDBClient).Atlas20231001002
connV2 := meta.(*config.MongoDBClient).AtlasV2
projectID := d.Get("project_id").(string)
clusterName := d.Get("cluster_name").(string)
archiveID := d.Get("archive_id").(string)

archive, _, err := conn20231001002.OnlineArchiveApi.GetOnlineArchive(ctx, projectID, archiveID, clusterName).Execute()
archive, _, err := connV2.OnlineArchiveApi.GetOnlineArchive(ctx, projectID, archiveID, clusterName).Execute()

if err != nil {
return diag.FromErr(fmt.Errorf("error reading Online Archive datasource with id %s: %s", archiveID, err.Error()))
Expand All @@ -245,18 +245,18 @@ func dataSourceMongoDBAtlasOnlineArchiveRead(ctx context.Context, d *schema.Reso
}

func dataSourceMongoDBAtlasOnlineArchivesRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
conn20231001002 := meta.(*config.MongoDBClient).Atlas20231001002
connV2 := meta.(*config.MongoDBClient).AtlasV2

projectID := d.Get("project_id").(string)
clusterName := d.Get("cluster_name").(string)

archives, _, err := conn20231001002.OnlineArchiveApi.ListOnlineArchives(ctx, projectID, clusterName).Execute()
archives, _, err := connV2.OnlineArchiveApi.ListOnlineArchives(ctx, projectID, clusterName).Execute()

if err != nil {
return diag.FromErr(fmt.Errorf("error getting Online Archives list for project(%s) in cluster (%s): (%s)", projectID, clusterName, err.Error()))
}

input := archives.Results
input := archives.GetResults()
results := make([]map[string]any, 0, len(input))
for i := range input {
archiveData := fromOnlineArchiveToMap(&input[i])
Expand Down
121 changes: 55 additions & 66 deletions internal/service/onlinearchive/resource_online_archive.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import (
"github.com/mongodb/terraform-provider-mongodbatlas/internal/common/conversion"
"github.com/mongodb/terraform-provider-mongodbatlas/internal/config"
"github.com/mwielbut/pointy"
admin20231001002 "go.mongodb.org/atlas-sdk/v20231001002/admin"
"go.mongodb.org/atlas-sdk/v20231115005/admin"
)

const (
Expand All @@ -27,19 +27,19 @@ const (

func Resource() *schema.Resource {
return &schema.Resource{
Schema: getMongoDBAtlasOnlineArchiveSchema(),
CreateContext: resourceMongoDBAtlasOnlineArchiveCreate,
ReadContext: resourceMongoDBAtlasOnlineArchiveRead,
DeleteContext: resourceMongoDBAtlasOnlineArchiveDelete,
UpdateContext: resourceMongoDBAtlasOnlineArchiveUpdate,
Schema: resourceSchema(),
CreateContext: resourceCreate,
ReadContext: resourceRead,
DeleteContext: resourceDelete,
UpdateContext: resourceUpdate,
Importer: &schema.ResourceImporter{
StateContext: resourceMongoDBAtlasOnlineArchiveImportState,
StateContext: resourceImport,
},
}
}

// https://docs.atlas.mongodb.com/reference/api/online-archive-create-one
func getMongoDBAtlasOnlineArchiveSchema() map[string]*schema.Schema {
func resourceSchema() map[string]*schema.Schema {
return map[string]*schema.Schema{
// argument values
"project_id": {
Expand Down Expand Up @@ -217,13 +217,13 @@ func getMongoDBAtlasOnlineArchiveSchema() map[string]*schema.Schema {
}
}

func resourceMongoDBAtlasOnlineArchiveCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
conn20231001002 := meta.(*config.MongoDBClient).Atlas20231001002
func resourceCreate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
connV2 := meta.(*config.MongoDBClient).AtlasV2
projectID := d.Get("project_id").(string)
clusterName := d.Get("cluster_name").(string)

inputRequest := mapToArchivePayload(d)
outputRequest, _, err := conn20231001002.OnlineArchiveApi.CreateOnlineArchive(ctx, projectID, clusterName, &inputRequest).Execute()
outputRequest, _, err := connV2.OnlineArchiveApi.CreateOnlineArchive(ctx, projectID, clusterName, &inputRequest).Execute()

if err != nil {
return diag.FromErr(fmt.Errorf(errorOnlineArchivesCreate, err))
Expand All @@ -241,7 +241,7 @@ func resourceMongoDBAtlasOnlineArchiveCreate(ctx context.Context, d *schema.Reso
stateConf := &retry.StateChangeConf{
Pending: []string{"PENDING", "ARCHIVING", "PAUSING", "PAUSED", "ORPHANED", "REPEATING"},
Target: []string{"IDLE", "ACTIVE"},
Refresh: resourceOnlineRefreshFunc(ctx, projectID, clusterName, archiveID, conn20231001002),
Refresh: resourceOnlineRefreshFunc(ctx, projectID, clusterName, archiveID, connV2),
Timeout: 3 * time.Hour,
MinTimeout: 1 * time.Minute,
Delay: 3 * time.Minute,
Expand All @@ -254,10 +254,10 @@ func resourceMongoDBAtlasOnlineArchiveCreate(ctx context.Context, d *schema.Reso
}
}

return resourceMongoDBAtlasOnlineArchiveRead(ctx, d, meta)
return resourceRead(ctx, d, meta)
}

func resourceOnlineRefreshFunc(ctx context.Context, projectID, clusterName, archiveID string, client *admin20231001002.APIClient) retry.StateRefreshFunc {
func resourceOnlineRefreshFunc(ctx context.Context, projectID, clusterName, archiveID string, client *admin.APIClient) retry.StateRefreshFunc {
return func() (any, string, error) {
c, resp, err := client.OnlineArchiveApi.GetOnlineArchive(ctx, projectID, archiveID, clusterName).Execute()

Expand Down Expand Up @@ -285,15 +285,15 @@ func resourceOnlineRefreshFunc(ctx context.Context, projectID, clusterName, arch
}
}

func resourceMongoDBAtlasOnlineArchiveRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
conn20231001002 := meta.(*config.MongoDBClient).Atlas20231001002
func resourceRead(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
connV2 := meta.(*config.MongoDBClient).AtlasV2
ids := conversion.DecodeStateID(d.Id())

archiveID := ids["archive_id"]
projectID := ids["project_id"]
clusterName := ids["cluster_name"]

onlineArchive, resp, err := conn20231001002.OnlineArchiveApi.GetOnlineArchive(context.Background(), projectID, archiveID, clusterName).Execute()
onlineArchive, resp, err := connV2.OnlineArchiveApi.GetOnlineArchive(context.Background(), projectID, archiveID, clusterName).Execute()
if err != nil {
if resp != nil && resp.StatusCode == http.StatusNotFound {
d.SetId("")
Expand All @@ -312,7 +312,7 @@ func resourceMongoDBAtlasOnlineArchiveRead(ctx context.Context, d *schema.Resour
return nil
}

func resourceMongoDBAtlasOnlineArchiveDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
func resourceDelete(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
conn := meta.(*config.MongoDBClient).Atlas
ids := conversion.DecodeStateID(d.Id())
atlasID := ids["archive_id"]
Expand All @@ -332,8 +332,8 @@ func resourceMongoDBAtlasOnlineArchiveDelete(ctx context.Context, d *schema.Reso
return nil
}

func resourceMongoDBAtlasOnlineArchiveImportState(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
conn20231001002 := meta.(*config.MongoDBClient).Atlas20231001002
func resourceImport(ctx context.Context, d *schema.ResourceData, meta any) ([]*schema.ResourceData, error) {
connV2 := meta.(*config.MongoDBClient).AtlasV2
parts := strings.Split(d.Id(), "-")

var projectID, clusterName, archiveID string
Expand All @@ -350,7 +350,7 @@ func resourceMongoDBAtlasOnlineArchiveImportState(ctx context.Context, d *schema
projectID, clusterName, archiveID = parts[0], parts[1], parts[2]
}

outOnlineArchive, _, err := conn20231001002.OnlineArchiveApi.GetOnlineArchive(ctx, projectID, archiveID, clusterName).Execute()
outOnlineArchive, _, err := connV2.OnlineArchiveApi.GetOnlineArchive(ctx, projectID, archiveID, clusterName).Execute()

if err != nil {
return nil, fmt.Errorf("could not import Online Archive %s in project %s, error %s", archiveID, projectID, err.Error())
Expand All @@ -377,9 +377,9 @@ func resourceMongoDBAtlasOnlineArchiveImportState(ctx context.Context, d *schema
return []*schema.ResourceData{d}, nil
}

func mapToArchivePayload(d *schema.ResourceData) admin20231001002.BackupOnlineArchiveCreate {
func mapToArchivePayload(d *schema.ResourceData) admin.BackupOnlineArchiveCreate {
// shared input
requestInput := admin20231001002.BackupOnlineArchiveCreate{
requestInput := admin.BackupOnlineArchiveCreate{
DbName: d.Get("db_name").(string),
CollName: d.Get("coll_name").(string),
}
Expand All @@ -393,57 +393,55 @@ func mapToArchivePayload(d *schema.ResourceData) admin20231001002.BackupOnlineAr
requestInput.Schedule = mapSchedule(d)

if partitions, ok := d.GetOk("partition_fields"); ok {
list := partitions.([]any)

if len(list) > 0 {
partitionList := make([]admin20231001002.PartitionField, 0, len(list))
if list := partitions.([]any); len(list) > 0 {
partitionList := make([]admin.PartitionField, 0, len(list))
for _, partition := range list {
item := partition.(map[string]any)
query := admin20231001002.PartitionField{
query := admin.PartitionField{
FieldName: item["field_name"].(string),
Order: item["order"].(int),
}

if dbType, ok := item["field_type"]; ok && dbType != nil {
if dbType.(string) != "" {
query.FieldType = admin20231001002.PtrString(dbType.(string))
query.FieldType = admin.PtrString(dbType.(string))
}
}

partitionList = append(partitionList, query)
}

requestInput.PartitionFields = partitionList
requestInput.PartitionFields = &partitionList
}
}

return requestInput
}

func resourceMongoDBAtlasOnlineArchiveUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
conn20231001002 := meta.(*config.MongoDBClient).Atlas20231001002
func resourceUpdate(ctx context.Context, d *schema.ResourceData, meta any) diag.Diagnostics {
connV2 := meta.(*config.MongoDBClient).AtlasV2

ids := conversion.DecodeStateID(d.Id())

atlasID := ids["archive_id"]
projectID := ids["project_id"]
clusterName := ids["cluster_name"]

if dataProcessRegionHasChange := d.HasChange("data_process_region"); dataProcessRegionHasChange {
return diag.FromErr(fmt.Errorf("error updating Mongo Online Archive id: %s, data_process_region can't be modified", atlasID))
}

// if the criteria or the pausedHasChange is enable then perform an update
pausedHasChange := d.HasChange("paused")
criteriaHasChange := d.HasChange("criteria")
dataExpirationRuleHasChange := d.HasChange("data_expiration_rule")
dataProcessRegionHasChange := d.HasChange("data_process_region")
scheduleHasChange := d.HasChange("schedule")

collectionTypeHasChange := d.HasChange("collection_type")

// nothing to do, let's go
if !pausedHasChange && !criteriaHasChange && !collectionTypeHasChange && !scheduleHasChange && !dataExpirationRuleHasChange && !dataProcessRegionHasChange {
if !pausedHasChange && !criteriaHasChange && !collectionTypeHasChange && !scheduleHasChange && !dataExpirationRuleHasChange {
return nil
}

request := admin20231001002.BackupOnlineArchive{}
request := admin.BackupOnlineArchive{}

// reading current value
if pausedHasChange {
Expand All @@ -459,39 +457,30 @@ func resourceMongoDBAtlasOnlineArchiveUpdate(ctx context.Context, d *schema.Reso
newExpirationRule := mapDataExpirationRule(d)
if newExpirationRule == nil {
// expiration rule has been removed from tf config, empty dataExpirationRule object needs to be sent in patch request
request.DataExpirationRule = &admin20231001002.DataExpirationRule{}
request.DataExpirationRule = &admin.DataExpirationRule{}
} else {
request.DataExpirationRule = newExpirationRule
}
}

if dataProcessRegionHasChange {
newDataProcessRegion := mapDataProcessRegion(d)
if newDataProcessRegion == nil {
request.DataProcessRegion = &admin20231001002.DataProcessRegion{}
} else {
request.DataProcessRegion = newDataProcessRegion
}
}

if scheduleHasChange {
request.Schedule = mapSchedule(d)
}

if collType := d.Get("collection_type").(string); collectionTypeHasChange && collType != "" {
request.CollectionType = admin20231001002.PtrString(collType)
request.CollectionType = admin.PtrString(collType)
}

_, _, err := conn20231001002.OnlineArchiveApi.UpdateOnlineArchive(ctx, projectID, atlasID, clusterName, &request).Execute()
_, _, err := connV2.OnlineArchiveApi.UpdateOnlineArchive(ctx, projectID, atlasID, clusterName, &request).Execute()

if err != nil {
return diag.FromErr(fmt.Errorf("error updating Mongo Online Archive id: %s %s", atlasID, err.Error()))
}

return resourceMongoDBAtlasOnlineArchiveRead(ctx, d, meta)
return resourceRead(ctx, d, meta)
}

func fromOnlineArchiveToMap(in *admin20231001002.BackupOnlineArchive) map[string]any {
func fromOnlineArchiveToMap(in *admin.BackupOnlineArchive) map[string]any {
// computed attribute
schemaVals := map[string]any{
"cluster_name": in.ClusterName,
Expand Down Expand Up @@ -567,7 +556,7 @@ func fromOnlineArchiveToMap(in *admin20231001002.BackupOnlineArchive) map[string
schemaVals["data_process_region"] = []any{dataProcessRegion}
}

partitionFields := in.PartitionFields
partitionFields := in.GetPartitionFields()
if len(partitionFields) == 0 {
return schemaVals
}
Expand All @@ -586,10 +575,10 @@ func fromOnlineArchiveToMap(in *admin20231001002.BackupOnlineArchive) map[string
return schemaVals
}

func mapDataExpirationRule(d *schema.ResourceData) *admin20231001002.DataExpirationRule {
func mapDataExpirationRule(d *schema.ResourceData) *admin.DataExpirationRule {
if dataExpireRules, ok := d.GetOk("data_expiration_rule"); ok && len(dataExpireRules.([]any)) > 0 {
dataExpireRule := dataExpireRules.([]any)[0].(map[string]any)
result := admin20231001002.DataExpirationRule{}
result := admin.DataExpirationRule{}
if expireAfterDays, ok := dataExpireRule["expire_after_days"]; ok {
result.ExpireAfterDays = pointy.Int(expireAfterDays.(int))
}
Expand All @@ -598,10 +587,10 @@ func mapDataExpirationRule(d *schema.ResourceData) *admin20231001002.DataExpirat
return nil
}

func mapDataProcessRegion(d *schema.ResourceData) *admin20231001002.DataProcessRegion {
func mapDataProcessRegion(d *schema.ResourceData) *admin.CreateDataProcessRegion {
if dataProcessRegions, ok := d.GetOk("data_process_region"); ok && len(dataProcessRegions.([]any)) > 0 {
dataProcessRegion := dataProcessRegions.([]any)[0].(map[string]any)
result := admin20231001002.DataProcessRegion{}
result := admin.CreateDataProcessRegion{}
if cloudProvider, ok := dataProcessRegion["cloud_provider"]; ok {
result.CloudProvider = pointy.String(cloudProvider.(string))
}
Expand All @@ -613,43 +602,43 @@ func mapDataProcessRegion(d *schema.ResourceData) *admin20231001002.DataProcessR
return nil
}

func mapCriteria(d *schema.ResourceData) admin20231001002.Criteria {
func mapCriteria(d *schema.ResourceData) admin.Criteria {
criteriaList := d.Get("criteria").([]any)

criteria := criteriaList[0].(map[string]any)

criteriaInput := admin20231001002.Criteria{
Type: admin20231001002.PtrString(criteria["type"].(string)),
criteriaInput := admin.Criteria{
Type: admin.PtrString(criteria["type"].(string)),
}

if criteriaInput.Type != nil && *criteriaInput.Type == "DATE" {
if dateField := criteria["date_field"].(string); dateField != "" {
criteriaInput.DateField = admin20231001002.PtrString(dateField)
criteriaInput.DateField = admin.PtrString(dateField)
}

criteriaInput.ExpireAfterDays = pointy.Int(criteria["expire_after_days"].(int))

// optional
if dformat, ok := criteria["date_format"]; ok && dformat.(string) != "" {
criteriaInput.DateFormat = admin20231001002.PtrString(dformat.(string))
criteriaInput.DateFormat = admin.PtrString(dformat.(string))
}
}

if criteriaInput.Type != nil && *criteriaInput.Type == "CUSTOM" {
if query := criteria["query"].(string); query != "" {
criteriaInput.Query = admin20231001002.PtrString(query)
criteriaInput.Query = admin.PtrString(query)
}
}

// Pending update client missing QUERY field
return criteriaInput
}

func mapSchedule(d *schema.ResourceData) *admin20231001002.OnlineArchiveSchedule {
func mapSchedule(d *schema.ResourceData) *admin.OnlineArchiveSchedule {
// scheduleInput := &matlas.OnlineArchiveSchedule{

// We have to provide schedule.type="DEFAULT" when the schedule block is not provided or removed
scheduleInput := &admin20231001002.OnlineArchiveSchedule{
scheduleInput := &admin.OnlineArchiveSchedule{
Type: scheduleTypeDefault,
}

Expand All @@ -668,7 +657,7 @@ func mapSchedule(d *schema.ResourceData) *admin20231001002.OnlineArchiveSchedule
}

scheduleTFConfig := scheduleTFConfigList[0].(map[string]any)
scheduleInput = &admin20231001002.OnlineArchiveSchedule{
scheduleInput = &admin.OnlineArchiveSchedule{
Type: scheduleTFConfig["type"].(string),
}

Expand Down
Loading

0 comments on commit f65f3fb

Please sign in to comment.