Skip to content

Commit

Permalink
servicefabricmanagedcluster: updating to use the ThenPoll methods
Browse files Browse the repository at this point in the history
  • Loading branch information
tombuildsstuff committed Jan 26, 2024
1 parent 0771a67 commit b9828a0
Showing 1 changed file with 49 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -274,14 +274,8 @@ func (k ClusterResource) Create() sdk.ResourceFunc {
return metadata.ResourceRequiresImport("azurerm_service_fabric_managed_cluster", managedClusterId)
}

resp, err := clusterClient.CreateOrUpdate(ctx, managedClusterId, cluster)
if err != nil {
return fmt.Errorf("while creating cluster %q: %+v", model.Name, err)
}
// Wait for the cluster creation operation to be completed
err = resp.Poller.PollUntilDone()
if err != nil {
return fmt.Errorf("while waiting for cluster %q to get created: : %+v", model.Name, err)
if err := clusterClient.CreateOrUpdateThenPoll(ctx, managedClusterId, cluster); err != nil {
return fmt.Errorf("creating %s: %+v", managedClusterId, err)
}

toDelete := make([]string, 0)
Expand Down Expand Up @@ -309,68 +303,27 @@ func (k ClusterResource) Create() sdk.ResourceFunc {
}
}

deleteResponses := make([]nodetype.DeleteOperationResponse, 0)
// Delete the old nodetypes
for _, nt := range toDelete {
resp, err := nodeTypeClient.Delete(ctx, nodetype.NewNodeTypeID(subscriptionId, model.ResourceGroup, model.Name, nt))
if err != nil {
return fmt.Errorf("while deleting node type %q of cluster %q: %+v", nt, model.Name, err)
}

if resp.HttpResponse != nil {
deleteResponses = append(deleteResponses, resp)
// Delete the old Node Types
for _, nodeType := range toDelete {
nodeTypeId := nodetype.NewNodeTypeID(subscriptionId, model.ResourceGroup, model.Name, nodeType)
if err := nodeTypeClient.DeleteThenPoll(ctx, nodeTypeId); err != nil {
return fmt.Errorf("deleting %s: %+v", nodeTypeId, err)
}
}

if len(deleteResponses) > 0 {
lastResp := deleteResponses[len(model.NodeTypes)-1]
if err = lastResp.Poller.PollUntilDone(); err != nil {
return fmt.Errorf("while polling for deletion of node type %q in cluster %q: %+v", model.NodeTypes[len(model.NodeTypes)-1].Name, model.Name, err)
}

for idx, resp := range deleteResponses {
if idx == len(deleteResponses)-1 {
continue
}
if err = resp.Poller.PollUntilDone(); err != nil {
return fmt.Errorf("while polling for deletion of node type %q in cluster %q: %+v", model.NodeTypes[idx].Name, model.Name, err)
}
}
}

// Send all Create NodeType requests, and store all responses to a list.
nodeTypeResponses := make([]nodetype.CreateOrUpdateOperationResponse, len(model.NodeTypes))
for idx, nt := range model.NodeTypes {
// Then provision the Node Types
for _, nt := range model.NodeTypes {
nodeTypeProperties, err := expandNodeTypeProperties(&nt)
if err != nil {
return fmt.Errorf("while expanding node type %q: %+v", nt.Name, err)
}

nodeTypeId := nodetype.NewNodeTypeID(subscriptionId, model.ResourceGroup, model.Name, nt.Name)
nodeTypeInput := nodetype.NodeType{
Name: nil,
payload := nodetype.NodeType{
Properties: nodeTypeProperties,
}

if resp, err := nodeTypeClient.CreateOrUpdate(ctx, nodeTypeId, nodeTypeInput); err == nil {
nodeTypeResponses[idx] = resp
} else {
return fmt.Errorf("while adding node type %q to cluster %q: %+v", nt.Name, model.Name, err)
}
}

if len(nodeTypeResponses) > 0 {
lastResp := nodeTypeResponses[len(model.NodeTypes)-1]
if err = lastResp.Poller.PollUntilDone(); err != nil {
return fmt.Errorf("while polling for node type %q in cluster %q: %+v", model.NodeTypes[len(model.NodeTypes)-1].Name, model.Name, err)
}

for idx, resp := range nodeTypeResponses {
if idx == len(nodeTypeResponses)-1 {
continue
}
if err = resp.Poller.PollUntilDone(); err != nil {
return fmt.Errorf("while polling for node type %q in cluster %q: %+v", model.NodeTypes[idx].Name, model.Name, err)
}
if err := nodeTypeClient.CreateOrUpdateThenPoll(ctx, nodeTypeId, payload); err != nil {
return fmt.Errorf("adding %s: %+v", nodeTypeId, err)
}
}

Expand All @@ -385,34 +338,35 @@ func (k ClusterResource) Create() sdk.ResourceFunc {
func (k ClusterResource) Read() sdk.ResourceFunc {
return sdk.ResourceFunc{
Func: func(ctx context.Context, metadata sdk.ResourceMetaData) error {
resourceId, err := managedcluster.ParseManagedClusterID(metadata.ResourceData.Id())
if err != nil {
return fmt.Errorf("while parsing resourceID: %+v", err)
}
clusterClient := metadata.Client.ServiceFabricManaged.ManagedClusterClient
nodeTypeClient := metadata.Client.ServiceFabricManaged.NodeTypeClient

cluster, err := clusterClient.Get(ctx, *resourceId)
id, err := managedcluster.ParseManagedClusterID(metadata.ResourceData.Id())
if err != nil {
return err
}

cluster, err := clusterClient.Get(ctx, *id)
if err != nil {
if response.WasNotFound(cluster.HttpResponse) {
return metadata.MarkAsGone(resourceId)
return metadata.MarkAsGone(id)
}
return fmt.Errorf("while reading data for cluster %q: %+v", resourceId.ManagedClusterName, err)
return fmt.Errorf("while reading data for cluster %q: %+v", id.ManagedClusterName, err)
}

nts, err := nodeTypeClient.ListByManagedClustersComplete(ctx, nodetype.ManagedClusterId{
SubscriptionId: resourceId.SubscriptionId,
ResourceGroupName: resourceId.ResourceGroupName,
ManagedClusterName: resourceId.ManagedClusterName,
SubscriptionId: id.SubscriptionId,
ResourceGroupName: id.ResourceGroupName,
ManagedClusterName: id.ManagedClusterName,
})
if err != nil {
return fmt.Errorf("while listing NodeTypes for cluster %q: +%v", resourceId.ManagedClusterName, err)
return fmt.Errorf("while listing NodeTypes for cluster %q: +%v", id.ManagedClusterName, err)
}

model := flattenClusterProperties(cluster.Model)
// Password is read-only
model.Password = metadata.ResourceData.Get("password").(string)
model.ResourceGroup = resourceId.ResourceGroupName
model.ResourceGroup = id.ResourceGroupName
model.NodeTypes = make([]NodeType, 0)
for _, nt := range nts.Items {
provState := nt.Properties.ProvisioningState
Expand All @@ -438,14 +392,18 @@ func (k ClusterResource) Update() sdk.ResourceFunc {
clusterClient := metadata.Client.ServiceFabricManaged.ManagedClusterClient
nodeTypeClient := metadata.Client.ServiceFabricManaged.NodeTypeClient

subscriptionId := metadata.Client.Account.SubscriptionId
id, err := managedcluster.ParseManagedClusterID(metadata.ResourceData.Id())
if err != nil {
return err
}

managedClusterId := managedcluster.NewManagedClusterID(subscriptionId, model.ResourceGroup, model.Name)
cluster := managedcluster.ManagedCluster{
Location: model.Location,
Name: utils.String(model.Name),
Properties: expandClusterProperties(&model),
Sku: &managedcluster.Sku{Name: model.Sku},
Sku: &managedcluster.Sku{
Name: model.Sku,
},
}

tagsMap := make(map[string]string)
Expand All @@ -454,14 +412,8 @@ func (k ClusterResource) Update() sdk.ResourceFunc {
}
cluster.Tags = &tagsMap

resp, err := clusterClient.CreateOrUpdate(ctx, managedClusterId, cluster)
if err != nil {
return fmt.Errorf("while creating cluster %q: %+v", model.Name, err)
}
// Wait for the cluster creation operation to be completed
err = resp.Poller.PollUntilDone()
if err != nil {
return fmt.Errorf("while waiting for cluster %q to get created: : %+v", model.Name, err)
if err := clusterClient.CreateOrUpdateThenPoll(ctx, *id, cluster); err != nil {
return fmt.Errorf("updating %s: %+v", *id, err)
}

toDelete := make([]string, 0)
Expand Down Expand Up @@ -489,68 +441,27 @@ func (k ClusterResource) Update() sdk.ResourceFunc {
}
}

deleteResponses := make([]nodetype.DeleteOperationResponse, 0)
// Delete the old nodetypes
for _, nt := range toDelete {
resp, err := nodeTypeClient.Delete(ctx, nodetype.NewNodeTypeID(subscriptionId, model.ResourceGroup, model.Name, nt))
if err != nil {
return fmt.Errorf("while deleting node type %q of cluster %q: %+v", nt, model.Name, err)
}
// Delete the old Node Types
for _, nodeType := range toDelete {
nodeTypeId := nodetype.NewNodeTypeID(id.SubscriptionId, id.ResourceGroupName, id.ManagedClusterName, nodeType)

if resp.HttpResponse != nil {
deleteResponses = append(deleteResponses, resp)
if err := nodeTypeClient.DeleteThenPoll(ctx, nodeTypeId); err != nil {
return fmt.Errorf("deleting %s: %+v", nodeTypeId, err)
}
}

if len(deleteResponses) > 0 {
lastResp := deleteResponses[len(model.NodeTypes)-1]
if err = lastResp.Poller.PollUntilDone(); err != nil {
return fmt.Errorf("while polling for deletion of node type %q in cluster %q: %+v", model.NodeTypes[len(model.NodeTypes)-1].Name, model.Name, err)
}

for idx, resp := range deleteResponses {
if idx == len(deleteResponses)-1 {
continue
}
if err = resp.Poller.PollUntilDone(); err != nil {
return fmt.Errorf("while polling for deletion of node type %q in cluster %q: %+v", model.NodeTypes[idx].Name, model.Name, err)
}
}
}

// Send all Create NodeType requests, and store all responses to a list.
nodeTypeResponses := make([]nodetype.CreateOrUpdateOperationResponse, len(model.NodeTypes))
for idx, nt := range model.NodeTypes {
nodeTypeProperties, err := expandNodeTypeProperties(&nt)
// Ensure the remaining Node Types are up-to-date
for _, nodeType := range model.NodeTypes {
props, err := expandNodeTypeProperties(&nodeType)
if err != nil {
return fmt.Errorf("while expanding node type %q: %+v", nt.Name, err)
}
nodeTypeId := nodetype.NewNodeTypeID(subscriptionId, model.ResourceGroup, model.Name, nt.Name)
nodeTypeInput := nodetype.NodeType{
Name: nil,
Properties: nodeTypeProperties,
return fmt.Errorf("while expanding node type %q: %+v", nodeType.Name, err)
}

if resp, err := nodeTypeClient.CreateOrUpdate(ctx, nodeTypeId, nodeTypeInput); err == nil {
nodeTypeResponses[idx] = resp
} else {
return fmt.Errorf("while adding node type %q to cluster %q: %+v", nt.Name, model.Name, err)
}
}

if len(nodeTypeResponses) > 0 {
lastResp := nodeTypeResponses[len(model.NodeTypes)-1]
if err = lastResp.Poller.PollUntilDone(); err != nil {
return fmt.Errorf("while polling for node type %q in cluster %q: %+v", model.NodeTypes[len(model.NodeTypes)-1].Name, model.Name, err)
nodeTypeId := nodetype.NewNodeTypeID(id.SubscriptionId, id.ResourceGroupName, id.ManagedClusterName, nodeType.Name)
payload := nodetype.NodeType{
Properties: props,
}

for idx, resp := range nodeTypeResponses {
if idx == len(nodeTypeResponses)-1 {
continue
}
if err = resp.Poller.PollUntilDone(); err != nil {
return fmt.Errorf("while polling for node type %q in cluster %q: %+v", model.NodeTypes[idx].Name, model.Name, err)
}
if err := nodeTypeClient.CreateOrUpdateThenPoll(ctx, nodeTypeId, payload); err != nil {
return fmt.Errorf("adding/updating %s: %+v", nodeTypeId, err)
}
}

Expand Down

0 comments on commit b9828a0

Please sign in to comment.