Skip to content

Commit

Permalink
reformatting ensure conditionals
Browse files Browse the repository at this point in the history
  • Loading branch information
jpflueger committed Apr 2, 2020
1 parent 9e0ceeb commit dc533b2
Showing 1 changed file with 66 additions and 55 deletions.
121 changes: 66 additions & 55 deletions pkg/resourcemanager/cosmosdbs/cosmosdb_reconcile.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,81 +23,92 @@ func (m *AzureCosmosDBManager) Ensure(ctx context.Context, obj runtime.Object, o
return false, err
}

// convert kube labels to expected tag format
tags := map[string]*string{}
for k, v := range instance.GetLabels() {
value := v
tags[k] = &value
hash := helpers.Hash256(instance.Spec)
if instance.Status.SpecHash != hash {
// need to push a create or update
instance.Status.SpecHash = hash
} else if instance.Status.Provisioned {
// provisioned and no changes needed
instance.Status.RequestedAt = nil
return true, nil
} else if instance.Status.Provisioning {
// get the instance and update status
db, azerr := m.GetCosmosDB(ctx, instance.Spec.ResourceGroup, instance.Name)
if azerr == nil {
instance.Status.ResourceId = *db.ID
instance.Status.State = *db.ProvisioningState

if instance.Status.State == "Creating" {
instance.Status.Message = "Waiting for resource to finish creation"
return false, nil
}

if instance.Status.State == "Succeeded" {
instance.Status.Message = resourcemanager.SuccessMsg
instance.Status.Provisioning = false
instance.Status.Provisioned = true
return true, nil
}
} else if azerr.Type == errhelp.ResourceGroupNotFoundErrorCode {
instance.Status.Provisioning = false
instance.Status.Message = fmt.Sprintf("Waiting for resource group '%s' to be available", instance.Spec.ResourceGroup)
instance.Status.State = "Waiting"
return false, nil
} else if azerr.Type == errhelp.ResourceNotFound {
exists, azerr := m.CheckNameExistsCosmosDB(ctx, instance.Name)
if azerr != nil {
instance.Status.Provisioning = false
instance.Status.Message = "Unexpected error occurred during resource request"
instance.Status.State = "Failed"
return false, err
} else if exists {
// get request returned resource not found and the name already exists
// so it must exist in a different resource group, user must fix it
instance.Status.Provisioning = false
instance.Status.Message = "CosmosDB name already exists"
instance.Status.State = "Failed"
return true, fmt.Errorf("cosmosdb name already exists")
}
} else {
instance.Status.Provisioning = false
instance.Status.Message = azerr.Error()
return false, azerr.Original
}
}

hash := helpers.Hash256(instance.Spec)
instance.Status.Provisioning = true

tags := helpers.LabelsToTags(instance.GetLabels())
accountName := instance.ObjectMeta.Name
groupName := instance.Spec.ResourceGroup
location := instance.Spec.Location
kind := instance.Spec.Kind
dbType := instance.Spec.Properties.DatabaseAccountOfferType

// get the database to see if it exists
db, azerr := m.GetCosmosDB(ctx, groupName, accountName)
if azerr == nil {
instance.Status.ResourceId = *db.ID
instance.Status.State = *db.ProvisioningState

switch *db.ProvisioningState {
case "Creating":
return creatingOrUpdating(&instance.Status)
case "Succeeded":
if instance.Status.SpecHash == hash {
return success(&instance.Status)
}
// needs update, allow to fall through
instance.Status.State = "Updating"
case "Deleting":
// previous delete hasn't finished, try again later
return waitingForDelete(&instance.Status)
default:
//TODO: how should we handle unknown states?
return true, nil
}
} else if azerr.Type == errhelp.ResourceNotFound {
exists, azerr := m.CheckNameExistsCosmosDB(ctx, accountName)
if azerr != nil {
return unexpectedError(&instance.Status, azerr.Original)
}
if exists {
// get request returned resource not found and the name already exists
// so it must exist in a different resource group, user must fix it
return nameAlreadyExists(&instance.Status)
}
instance.Status.State = "Creating"
} else if azerr.Type == errhelp.ResourceGroupNotFoundErrorCode {
return waitingForParent(&instance.Status, groupName)
}

// create the database
instance.Status.Provisioning = true
instance.Status.Provisioned = false
instance.Status.ResourceId = ""
instance.Status.SpecHash = ""
instance.Status.Message = ""
db, azerr = m.CreateOrUpdateCosmosDB(ctx, groupName, accountName, location, kind, dbType, tags)
db, azerr := m.CreateOrUpdateCosmosDB(ctx, groupName, accountName, location, kind, dbType, tags)

// everything is in a created/updated state
if azerr == nil {
instance.Status.Provisioned = true
instance.Status.Provisioning = false
instance.Status.Message = resourcemanager.SuccessMsg
instance.Status.State = "Succeeded"
instance.Status.ResourceId = *db.ID
instance.Status.SpecHash = hash
return success(&instance.Status)
return true, nil
}

switch azerr.Type {

case errhelp.AsyncOpIncompleteError:
return creatingOrUpdating(&instance.Status)
instance.Status.Message = "Resource request successfully submitted to Azure"
instance.Status.State = "Creating"

case errhelp.InvalidResourceLocation:
instance.Status.Provisioning = false
return true, fmt.Errorf(azerr.Reason)
default:
return unexpectedError(&instance.Status, err)

}
return false, nil
}

// Delete drops cosmosdb
Expand Down

0 comments on commit dc533b2

Please sign in to comment.