Skip to content

Commit

Permalink
chore: refactor name and parameter for classic/hcp
Browse files Browse the repository at this point in the history
  • Loading branch information
gdbranco committed Aug 8, 2024
1 parent 18b4511 commit 1b3c7e5
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 59 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ func (r *ClusterRosaClassicDatasource) Read(ctx context.Context, request datasou
object := get.Body()

// Save the state:
err = populateRosaClassicClusterState(ctx, object, state, common.DefaultHttpClient{})
err = populateRosaClassicClusterState(ctx, object, state)
if err != nil {
response.Diagnostics.AddError(
"Can't populate cluster state",
Expand Down
46 changes: 22 additions & 24 deletions provider/clusterrosa/classic/cluster_rosa_classic_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -792,8 +792,8 @@ func (r *ClusterRosaClassicResource) Create(ctx context.Context, request resourc
tflog.Debug(ctx, "begin create()")

// Get the plan:
state := &ClusterRosaClassicState{}
diags := request.Plan.Get(ctx, state)
plan := &ClusterRosaClassicState{}
diags := request.Plan.Get(ctx, plan)
response.Diagnostics.Append(diags...)
if response.Diagnostics.HasError() {
return
Expand All @@ -802,7 +802,7 @@ func (r *ClusterRosaClassicResource) Create(ctx context.Context, request resourc

// In case version with "openshift-v" prefix was used here,
// Give a meaningful message to inform the user that it not supported any more
if common.HasValue(state.Version) && strings.HasPrefix(state.Version.ValueString(), rosa.VersionPrefix) {
if common.HasValue(plan.Version) && strings.HasPrefix(plan.Version.ValueString(), rosa.VersionPrefix) {
response.Diagnostics.AddError(
summary,
"Openshift version must be provided without the \"openshift-v\" prefix",
Expand All @@ -811,44 +811,44 @@ func (r *ClusterRosaClassicResource) Create(ctx context.Context, request resourc
}

channelGroup := consts.DefaultChannelGroup
if common.HasValue(state.ChannelGroup) {
channelGroup = state.ChannelGroup.ValueString()
if common.HasValue(plan.ChannelGroup) {
channelGroup = plan.ChannelGroup.ValueString()
}
desiredVersion := ""
if common.HasValue(state.Version) {
desiredVersion = state.Version.ValueString()
if common.HasValue(plan.Version) {
desiredVersion = plan.Version.ValueString()
}
version, err := r.GetAndValidateVersionInChannelGroup(ctx, rosaTypes.Classic, channelGroup, desiredVersion)
if err != nil {
response.Diagnostics.AddError(
summary,
fmt.Sprintf(
"Can't build cluster with name '%s': %v",
state.Name.ValueString(), err,
plan.Name.ValueString(), err,
),
)
return
}

err = validateHttpTokensVersion(ctx, state, version)
err = validateHttpTokensVersion(ctx, plan, version)
if err != nil {
response.Diagnostics.AddError(
summary,
fmt.Sprintf(
"Can't build cluster with name '%s': %v",
state.Name.ValueString(), err,
plan.Name.ValueString(), err,
),
)
return
}

object, err := createClassicClusterObject(ctx, state, diags)
object, err := createClassicClusterObject(ctx, plan, diags)
if err != nil {
response.Diagnostics.AddError(
summary,
fmt.Sprintf(
"Can't build cluster with name '%s': %v",
state.Name.ValueString(), err,
plan.Name.ValueString(), err,
),
)
return
Expand All @@ -860,15 +860,15 @@ func (r *ClusterRosaClassicResource) Create(ctx context.Context, request resourc
summary,
fmt.Sprintf(
"Can't create cluster with name '%s': %v",
state.Name.ValueString(), err,
plan.Name.ValueString(), err,
),
)
return
}
object = add.Body()

// Save initial state:
err = populateRosaClassicClusterState(ctx, object, state, common.DefaultHttpClient{})
err = populateRosaClassicClusterState(ctx, object, plan)
if err != nil {
response.Diagnostics.AddError(
"Can't populate cluster state",
Expand All @@ -878,24 +878,24 @@ func (r *ClusterRosaClassicResource) Create(ctx context.Context, request resourc
)
return
}
response.Diagnostics.Append(response.State.Set(ctx, plan)...)

if common.HasValue(state.WaitForCreateComplete) && state.WaitForCreateComplete.ValueBool() {
if common.HasValue(plan.WaitForCreateComplete) && plan.WaitForCreateComplete.ValueBool() {
object, err = r.ClusterWait.WaitForClusterToBeReady(ctx, object.ID(), rosa.DefaultWaitTimeoutInMinutes)
if err != nil {
response.Diagnostics.AddError(
"Waiting for cluster creation finished with error",
fmt.Sprintf("Waiting for cluster creation finished with the error %v", err),
)
if object == nil {
diags = response.State.Set(ctx, state)
response.Diagnostics.Append(diags...)
response.Diagnostics.Append(response.State.Set(ctx, plan)...)
return
}
}
}

// Save the state post wait completion:
err = populateRosaClassicClusterState(ctx, object, state, common.DefaultHttpClient{})
err = populateRosaClassicClusterState(ctx, object, plan)
if err != nil {
response.Diagnostics.AddError(
"Can't populate cluster state",
Expand All @@ -905,9 +905,7 @@ func (r *ClusterRosaClassicResource) Create(ctx context.Context, request resourc
)
return
}

diags = response.State.Set(ctx, state)
response.Diagnostics.Append(diags...)
response.Diagnostics.Append(response.State.Set(ctx, plan)...)
}

func (r *ClusterRosaClassicResource) Read(ctx context.Context, request resource.ReadRequest,
Expand Down Expand Up @@ -944,7 +942,7 @@ func (r *ClusterRosaClassicResource) Read(ctx context.Context, request resource.
object := get.Body()

// Save the state:
err = populateRosaClassicClusterState(ctx, object, state, common.DefaultHttpClient{})
err = populateRosaClassicClusterState(ctx, object, state)
if err != nil {
response.Diagnostics.AddError(
"Can't populate cluster state",
Expand Down Expand Up @@ -1137,7 +1135,7 @@ func (r *ClusterRosaClassicResource) Update(ctx context.Context, request resourc
object := update.Body()

// Update the state:
err = populateRosaClassicClusterState(ctx, object, plan, common.DefaultHttpClient{})
err = populateRosaClassicClusterState(ctx, object, plan)
if err != nil {
response.Diagnostics.AddError(
"Can't populate cluster state",
Expand Down Expand Up @@ -1409,7 +1407,7 @@ func (r *ClusterRosaClassicResource) ImportState(ctx context.Context, request re
}

// populateRosaClassicClusterState copies the data from the API object to the Terraform state.
func populateRosaClassicClusterState(ctx context.Context, object *cmv1.Cluster, state *ClusterRosaClassicState, httpClient common.HttpClient) error {
func populateRosaClassicClusterState(ctx context.Context, object *cmv1.Cluster, state *ClusterRosaClassicState) error {
state.ID = types.StringValue(object.ID())
state.ExternalID = types.StringValue(object.ExternalID())
object.API()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ var _ = Describe("Rosa Classic Sts cluster", func() {

clusterObject, err := cmv1.UnmarshalCluster(clusterJsonString)
Expect(err).ToNot(HaveOccurred())
Expect(populateRosaClassicClusterState(context.Background(), clusterObject, clusterState, mockHttpClient)).To(Succeed())
Expect(populateRosaClassicClusterState(context.Background(), clusterObject, clusterState)).To(Succeed())

Expect(clusterState.ID.ValueString()).To(Equal(clusterId))
Expect(clusterState.CloudRegion.ValueString()).To(Equal(regionId))
Expand Down Expand Up @@ -282,7 +282,7 @@ var _ = Describe("Rosa Classic Sts cluster", func() {
clusterObject, err := cmv1.UnmarshalCluster(clusterJsonString)
Expect(err).ToNot(HaveOccurred())

err = populateRosaClassicClusterState(context.Background(), clusterObject, clusterState, mockHttpClient)
err = populateRosaClassicClusterState(context.Background(), clusterObject, clusterState)
Expect(err).ToNot(HaveOccurred())
Expect(clusterState.Sts.OIDCEndpointURL.ValueString()).To(Equal("nonce.com"))
})
Expand All @@ -298,7 +298,7 @@ var _ = Describe("Rosa Classic Sts cluster", func() {
clusterObject, err := cmv1.UnmarshalCluster(clusterJsonString)
Expect(err).ToNot(HaveOccurred())

err = populateRosaClassicClusterState(context.Background(), clusterObject, clusterState, mockHttpClient)
err = populateRosaClassicClusterState(context.Background(), clusterObject, clusterState)
Expect(err).ToNot(HaveOccurred())
Expect(clusterState.Sts.Thumbprint.ValueString()).To(Equal(""))
})
Expand Down
2 changes: 1 addition & 1 deletion provider/clusterrosa/hcp/datasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ func (r *ClusterRosaHcpDatasource) Read(ctx context.Context, request datasource.
object := get.Body()

// Save the state:
err = populateRosaHcpClusterState(ctx, object, state, common.DefaultHttpClient{})
err = populateRosaHcpClusterState(ctx, object, state)
if err != nil {
response.Diagnostics.AddError(
"Can't populate cluster state",
Expand Down
51 changes: 24 additions & 27 deletions provider/clusterrosa/hcp/resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,16 +624,16 @@ func (r *ClusterRosaHcpResource) Create(ctx context.Context, request resource.Cr
tflog.Debug(ctx, "begin create()")

// Get the plan:
state := &ClusterRosaHcpState{}
diags := request.Plan.Get(ctx, state)
plan := &ClusterRosaHcpState{}
diags := request.Plan.Get(ctx, plan)
response.Diagnostics.Append(diags...)
if response.Diagnostics.HasError() {
return
}
summary := "Can't build cluster"

shouldWaitCreationComplete := common.BoolWithFalseDefault(state.WaitForCreateComplete)
shouldWaitComputeNodesComplete := common.BoolWithFalseDefault(state.WaitForStdComputeNodesComplete)
shouldWaitCreationComplete := common.BoolWithFalseDefault(plan.WaitForCreateComplete)
shouldWaitComputeNodesComplete := common.BoolWithFalseDefault(plan.WaitForStdComputeNodesComplete)
if shouldWaitComputeNodesComplete && !shouldWaitCreationComplete {
response.Diagnostics.AddError(
summary,
Expand All @@ -642,8 +642,8 @@ func (r *ClusterRosaHcpResource) Create(ctx context.Context, request resource.Cr
return
}

hasEtcdEncrpytion := common.BoolWithFalseDefault(state.EtcdEncryption)
hasEtcdKmsKeyArn := common.HasValue(state.EtcdKmsKeyArn) && state.EtcdKmsKeyArn.ValueString() != ""
hasEtcdEncrpytion := common.BoolWithFalseDefault(plan.EtcdEncryption)
hasEtcdKmsKeyArn := common.HasValue(plan.EtcdKmsKeyArn) && plan.EtcdKmsKeyArn.ValueString() != ""
if (!hasEtcdEncrpytion && hasEtcdKmsKeyArn) || (hasEtcdEncrpytion && !hasEtcdKmsKeyArn) {
response.Diagnostics.AddError(
summary,
Expand All @@ -654,7 +654,7 @@ func (r *ClusterRosaHcpResource) Create(ctx context.Context, request resource.Cr

// In case version with "openshift-v" prefix was used here,
// Give a meaningful message to inform the user that it not supported any more
if common.HasValue(state.Version) && strings.HasPrefix(state.Version.ValueString(), rosa.VersionPrefix) {
if common.HasValue(plan.Version) && strings.HasPrefix(plan.Version.ValueString(), rosa.VersionPrefix) {
response.Diagnostics.AddError(
summary,
"Openshift version must be provided without the \"openshift-v\" prefix",
Expand All @@ -663,32 +663,32 @@ func (r *ClusterRosaHcpResource) Create(ctx context.Context, request resource.Cr
}

channelGroup := consts.DefaultChannelGroup
if common.HasValue(state.ChannelGroup) {
channelGroup = state.ChannelGroup.ValueString()
if common.HasValue(plan.ChannelGroup) {
channelGroup = plan.ChannelGroup.ValueString()
}
desiredVersion := ""
if common.HasValue(state.Version) {
desiredVersion = state.Version.ValueString()
if common.HasValue(plan.Version) {
desiredVersion = plan.Version.ValueString()
}
_, err := r.GetAndValidateVersionInChannelGroup(ctx, rosaTypes.Hcp, channelGroup, desiredVersion)
if err != nil {
response.Diagnostics.AddError(
summary,
fmt.Sprintf(
"Can't build cluster with name '%s': %v",
state.Name.ValueString(), err,
plan.Name.ValueString(), err,
),
)
return
}

object, err := createHcpClusterObject(ctx, state, diags)
object, err := createHcpClusterObject(ctx, plan, diags)
if err != nil {
response.Diagnostics.AddError(
summary,
fmt.Sprintf(
"Can't build cluster with name '%s': %v",
state.Name.ValueString(), err,
plan.Name.ValueString(), err,
),
)
return
Expand All @@ -700,15 +700,15 @@ func (r *ClusterRosaHcpResource) Create(ctx context.Context, request resource.Cr
summary,
fmt.Sprintf(
"Can't create cluster with name '%s': %v",
state.Name.ValueString(), err,
plan.Name.ValueString(), err,
),
)
return
}
object = add.Body()

// Save initial state:
err = populateRosaHcpClusterState(ctx, object, state, common.DefaultHttpClient{})
err = populateRosaHcpClusterState(ctx, object, plan)
if err != nil {
response.Diagnostics.AddError(
"Can't populate cluster state",
Expand All @@ -718,6 +718,7 @@ func (r *ClusterRosaHcpResource) Create(ctx context.Context, request resource.Cr
)
return
}
response.Diagnostics.Append(response.State.Set(ctx, plan)...)

if shouldWaitCreationComplete {
tflog.Info(ctx, "Waiting for cluster to get ready")
Expand All @@ -728,8 +729,7 @@ func (r *ClusterRosaHcpResource) Create(ctx context.Context, request resource.Cr
fmt.Sprintf("Waiting for cluster creation finished with the error %v", err),
)
if object == nil {
diags = response.State.Set(ctx, state)
response.Diagnostics.Append(diags...)
response.Diagnostics.Append(response.State.Set(ctx, plan)...)
return
}
}
Expand All @@ -742,16 +742,15 @@ func (r *ClusterRosaHcpResource) Create(ctx context.Context, request resource.Cr
fmt.Sprintf("Waiting for std compute nodes completion finished with the error %v", err),
)
if object == nil {
diags = response.State.Set(ctx, state)
response.Diagnostics.Append(diags...)
response.Diagnostics.Append(response.State.Set(ctx, plan)...)
return
}
}
}
}

// Save the state post wait completion:
err = populateRosaHcpClusterState(ctx, object, state, common.DefaultHttpClient{})
err = populateRosaHcpClusterState(ctx, object, plan)
if err != nil {
response.Diagnostics.AddError(
"Can't populate cluster state",
Expand All @@ -761,9 +760,7 @@ func (r *ClusterRosaHcpResource) Create(ctx context.Context, request resource.Cr
)
return
}

diags = response.State.Set(ctx, state)
response.Diagnostics.Append(diags...)
response.Diagnostics.Append(response.State.Set(ctx, plan)...)
}

func (r *ClusterRosaHcpResource) Read(ctx context.Context, request resource.ReadRequest,
Expand Down Expand Up @@ -800,7 +797,7 @@ func (r *ClusterRosaHcpResource) Read(ctx context.Context, request resource.Read
object := get.Body()

// Save the state:
err = populateRosaHcpClusterState(ctx, object, state, common.DefaultHttpClient{})
err = populateRosaHcpClusterState(ctx, object, state)
if err != nil {
response.Diagnostics.AddError(
"Can't populate cluster state",
Expand Down Expand Up @@ -978,7 +975,7 @@ func (r *ClusterRosaHcpResource) Update(ctx context.Context, request resource.Up
object := update.Body()

// Update the state:
err = populateRosaHcpClusterState(ctx, object, plan, common.DefaultHttpClient{})
err = populateRosaHcpClusterState(ctx, object, plan)
if err != nil {
response.Diagnostics.AddError(
"Can't populate cluster state",
Expand Down Expand Up @@ -1242,7 +1239,7 @@ func (r *ClusterRosaHcpResource) ImportState(ctx context.Context, request resour
}

// populateRosaHcpClusterState copies the data from the API object to the Terraform state.
func populateRosaHcpClusterState(ctx context.Context, object *cmv1.Cluster, state *ClusterRosaHcpState, httpClient common.HttpClient) error {
func populateRosaHcpClusterState(ctx context.Context, object *cmv1.Cluster, state *ClusterRosaHcpState) error {
state.ID = types.StringValue(object.ID())
state.ExternalID = types.StringValue(object.ExternalID())
object.API()
Expand Down
Loading

0 comments on commit 1b3c7e5

Please sign in to comment.