Skip to content

Commit

Permalink
clean
Browse files Browse the repository at this point in the history
  • Loading branch information
ArthurVerrept committed Mar 6, 2024
1 parent ef97eb6 commit 48f771a
Show file tree
Hide file tree
Showing 8 changed files with 406 additions and 410 deletions.
16 changes: 8 additions & 8 deletions internal/provider/network_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,13 +105,13 @@ func (d *NetworkDataSource) Read(ctx context.Context, req datasource.ReadRequest
return
}

state.Id = types.StringValue(res.Network.Id)
state.DataCenterId = types.StringValue(res.Network.DataCenterId)
state.IPRange = types.StringValue(res.Network.IpRange)
state.Gateway = types.StringValue(res.Network.Gateway)
state.ExternalIPAddress = types.StringValue(res.Network.ExternalIpAddress)
state.InternalIPAddress = types.StringValue(res.Network.InternalIpAddress)

// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
resp.Diagnostics.Append(resp.State.Set(ctx, NetworkDataSourceModel{
Id: types.StringValue(res.Network.Id),
DataCenterId: types.StringValue(res.Network.DataCenterId),
IPRange: types.StringValue(res.Network.IpRange),
Gateway: types.StringValue(res.Network.Gateway),
ExternalIPAddress: types.StringValue(res.Network.ExternalIpAddress),
InternalIPAddress: types.StringValue(res.Network.InternalIpAddress),
})...)
}
273 changes: 138 additions & 135 deletions internal/provider/network_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -112,128 +112,8 @@ func (r *NetworkResource) Configure(ctx context.Context, req resource.ConfigureR
r.client = client
}

func waitForNetworkAvailable(ctx context.Context, projectID string, networkID string, c network.NetworkServiceClient) (*network.Network, error) {
refreshFunc := func() (interface{}, string, error) {
res, err := c.GetNetwork(ctx, &network.GetNetworkRequest{
Id: networkID,
ProjectId: projectID,
})
if err != nil {
if ok := helper.IsErrCode(err, codes.NotFound); ok {
tflog.Debug(ctx, fmt.Sprintf("Network %s in project %s not found: ", networkID, projectID))
return res, "done", nil
}
return nil, "", err
}

tflog.Trace(ctx, fmt.Sprintf("pending network %s in project %s state: %s", networkID, projectID, res.Network.ShortState))
return res, res.Network.ShortState, nil
}

tflog.Debug(ctx, fmt.Sprintf("waiting for network %s in project %s ", networkID, projectID))

stateConf := &helper.StateChangeConf{
Pending: []string{"clea", "clon", "dsrz", "epil", "hold", "hotp", "init", "migr", "pend", "prol", "save", "shut", "snap", "unkn"},
Target: []string{"boot", "done", "fail", "poff", "runn", "stop", "susp", "unde"},
Refresh: refreshFunc,
Timeout: 2 * time.Hour,
Delay: 1 * time.Second,
MinTimeout: 3 * time.Second,
}

if res, err := stateConf.WaitForState(ctx); err != nil {
return nil, fmt.Errorf("error waiting for network %s in project %s to become available: %w", networkID, projectID, err)
} else if res, ok := res.(*network.GetNetworkResponse); ok {
tflog.Trace(ctx, fmt.Sprintf("completed waiting for network %s in project %s (%s)", networkID, projectID, res.Network.ShortState))
return res.Network, nil
}

return nil, nil
}

func waitForNetworkStop(ctx context.Context, projectID string, networkID string, c network.NetworkServiceClient) (*network.Network, error) {
refreshFunc := func() (interface{}, string, error) {
res, err := c.GetNetwork(ctx, &network.GetNetworkRequest{
Id: networkID,
ProjectId: projectID,
})
if err != nil {
if ok := helper.IsErrCode(err, codes.NotFound); ok {
tflog.Debug(ctx, fmt.Sprintf("Network %s in project %s is done: ", networkID, projectID))
return res, "done", nil
}
tflog.Error(ctx, fmt.Sprintf("error getting network %s in project %s: %v", networkID, projectID, err))
return nil, "", err
}
if res.Network.ShortState == "" {
tflog.Debug(ctx, fmt.Sprintf("Network %s in project %s is stopped: ", networkID, projectID))
return res, "done", nil
}

tflog.Trace(ctx, fmt.Sprintf("pending network %s in project %s state: %s", networkID, projectID, res.Network.ShortState))
return res, res.Network.ShortState, nil
}

tflog.Debug(ctx, fmt.Sprintf("waiting for network %s in project %s ", networkID, projectID))

stateConf := &helper.StateChangeConf{
Pending: []string{"fail", "poff", "runn", "stop", "susp", "unde", "boot", "clea", "clon", "dsrz", "epil", "hold", "hotp", "init", "migr", "pend", "prol", "save", "shut", "snap", "unkn"},
Target: []string{"done"},
Refresh: refreshFunc,
Timeout: 20 * time.Minute,
MinTimeout: 3 * time.Second,
}

if _, err := stateConf.WaitForState(ctx); err != nil {
return nil, fmt.Errorf("error waiting for network %s in project %s to be stopped: %w", networkID, projectID, err)
}

return nil, nil
}

func waitForNetworkDelete(ctx context.Context, projectID string, networkID string, c network.NetworkServiceClient) (*network.Network, error) {
refreshFunc := func() (interface{}, string, error) {
res, err := c.GetNetwork(ctx, &network.GetNetworkRequest{
Id: networkID,
ProjectId: projectID,
})
if err != nil {
if ok := helper.IsErrCode(err, codes.NotFound); ok {
tflog.Debug(ctx, fmt.Sprintf("Network %s in project %s is done: ", networkID, projectID))
return res, "done", nil
}

tflog.Error(ctx, fmt.Sprintf("error getting network %s in project %s: %v", networkID, projectID, err))
return nil, "", err
}
if res.Network.ShortState == "" {
tflog.Debug(ctx, fmt.Sprintf("Network %s in project %s is stopped: ", networkID, projectID))
return res, "stop", nil
}

tflog.Trace(ctx, fmt.Sprintf("pending network %s in project %s state: %s", networkID, projectID, res.Network.ShortState))
return res, res.Network.ShortState, nil
}

tflog.Debug(ctx, fmt.Sprintf("waiting for network %s in project %s ", networkID, projectID))

stateConf := &helper.StateChangeConf{
Pending: []string{"fail", "poff", "runn", "stop", "susp", "unde", "boot", "clea", "clon", "dsrz", "epil", "hold", "hotp", "init", "migr", "pend", "prol", "save", "shut", "snap", "unkn"},
Target: []string{"done"},
Refresh: refreshFunc,
Timeout: 2 * time.Hour,
MinTimeout: 3 * time.Second,
}

if _, err := stateConf.WaitForState(ctx); err != nil {
return nil, fmt.Errorf("error waiting for network %s in project %s to be deleted: %w", networkID, projectID, err)
}

return nil, nil
}

func (r *NetworkResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) {
var state *NetworkResourceModel
var state NetworkResourceModel

// Read Terraform plan data into the model
resp.Diagnostics.Append(req.Plan.Get(ctx, &state)...)
Expand Down Expand Up @@ -265,15 +145,18 @@ func (r *NetworkResource) Create(ctx context.Context, req resource.CreateRequest
return
}

state.Gateway = types.StringValue(network.Gateway)
state.ExternalIPAddress = types.StringValue(network.ExternalIpAddress)
state.InternalIPAddress = types.StringValue(network.InternalIpAddress)

resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
resp.Diagnostics.Append(resp.State.Set(ctx, NetworkResourceModel{
ID: types.StringValue(network.Id),
DataCenterId: types.StringValue(network.DataCenterId),
IPRange: types.StringValue(network.IpRange),
Gateway: types.StringValue(network.Gateway),
ExternalIPAddress: types.StringValue(network.ExternalIpAddress),
InternalIPAddress: types.StringValue(network.InternalIpAddress),
})...)
}

func (r *NetworkResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var state *NetworkResourceModel
var state NetworkResourceModel

// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
Expand All @@ -297,15 +180,15 @@ func (r *NetworkResource) Read(ctx context.Context, req resource.ReadRequest, re
return
}

state.ID = types.StringValue(res.Network.Id)
state.DataCenterId = types.StringValue(res.Network.DataCenterId)
state.ExternalIPAddress = types.StringValue(res.Network.ExternalIpAddress)
state.InternalIPAddress = types.StringValue(res.Network.InternalIpAddress)
state.IPRange = types.StringValue(res.Network.IpRange)
state.Gateway = types.StringValue(res.Network.Gateway)

// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
resp.Diagnostics.Append(resp.State.Set(ctx, NetworkResourceModel{
ID: types.StringValue(res.Network.Id),
DataCenterId: types.StringValue(res.Network.DataCenterId),
ExternalIPAddress: types.StringValue(res.Network.ExternalIpAddress),
InternalIPAddress: types.StringValue(res.Network.InternalIpAddress),
IPRange: types.StringValue(res.Network.IpRange),
Gateway: types.StringValue(res.Network.Gateway),
})...)
}

func (r *NetworkResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
Expand Down Expand Up @@ -381,3 +264,123 @@ func (r *NetworkResource) Delete(ctx context.Context, req resource.DeleteRequest
func (r *NetworkResource) ImportState(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
}

func waitForNetworkAvailable(ctx context.Context, projectID string, networkID string, c network.NetworkServiceClient) (*network.Network, error) {
refreshFunc := func() (interface{}, string, error) {
res, err := c.GetNetwork(ctx, &network.GetNetworkRequest{
Id: networkID,
ProjectId: projectID,
})
if err != nil {
if ok := helper.IsErrCode(err, codes.NotFound); ok {
tflog.Debug(ctx, fmt.Sprintf("Network %s in project %s not found: ", networkID, projectID))
return res, "done", nil
}
return nil, "", err
}

tflog.Trace(ctx, fmt.Sprintf("pending network %s in project %s state: %s", networkID, projectID, res.Network.ShortState))
return res, res.Network.ShortState, nil
}

tflog.Debug(ctx, fmt.Sprintf("waiting for network %s in project %s ", networkID, projectID))

stateConf := &helper.StateChangeConf{
Pending: []string{"clea", "clon", "dsrz", "epil", "hold", "hotp", "init", "migr", "pend", "prol", "save", "shut", "snap", "unkn"},
Target: []string{"boot", "done", "fail", "poff", "runn", "stop", "susp", "unde"},
Refresh: refreshFunc,
Timeout: 2 * time.Hour,
Delay: 1 * time.Second,
MinTimeout: 3 * time.Second,
}

if res, err := stateConf.WaitForState(ctx); err != nil {
return nil, fmt.Errorf("error waiting for network %s in project %s to become available: %w", networkID, projectID, err)
} else if res, ok := res.(*network.GetNetworkResponse); ok {
tflog.Trace(ctx, fmt.Sprintf("completed waiting for network %s in project %s (%s)", networkID, projectID, res.Network.ShortState))
return res.Network, nil
}

return nil, nil
}

func waitForNetworkStop(ctx context.Context, projectID string, networkID string, c network.NetworkServiceClient) (*network.Network, error) {
refreshFunc := func() (interface{}, string, error) {
res, err := c.GetNetwork(ctx, &network.GetNetworkRequest{
Id: networkID,
ProjectId: projectID,
})
if err != nil {
if ok := helper.IsErrCode(err, codes.NotFound); ok {
tflog.Debug(ctx, fmt.Sprintf("Network %s in project %s is done: ", networkID, projectID))
return res, "done", nil
}
tflog.Error(ctx, fmt.Sprintf("error getting network %s in project %s: %v", networkID, projectID, err))
return nil, "", err
}
if res.Network.ShortState == "" {
tflog.Debug(ctx, fmt.Sprintf("Network %s in project %s is stopped: ", networkID, projectID))
return res, "done", nil
}

tflog.Trace(ctx, fmt.Sprintf("pending network %s in project %s state: %s", networkID, projectID, res.Network.ShortState))
return res, res.Network.ShortState, nil
}

tflog.Debug(ctx, fmt.Sprintf("waiting for network %s in project %s ", networkID, projectID))

stateConf := &helper.StateChangeConf{
Pending: []string{"fail", "poff", "runn", "stop", "susp", "unde", "boot", "clea", "clon", "dsrz", "epil", "hold", "hotp", "init", "migr", "pend", "prol", "save", "shut", "snap", "unkn"},
Target: []string{"done"},
Refresh: refreshFunc,
Timeout: 20 * time.Minute,
MinTimeout: 3 * time.Second,
}

if _, err := stateConf.WaitForState(ctx); err != nil {
return nil, fmt.Errorf("error waiting for network %s in project %s to be stopped: %w", networkID, projectID, err)
}

return nil, nil
}

func waitForNetworkDelete(ctx context.Context, projectID string, networkID string, c network.NetworkServiceClient) (*network.Network, error) {
refreshFunc := func() (interface{}, string, error) {
res, err := c.GetNetwork(ctx, &network.GetNetworkRequest{
Id: networkID,
ProjectId: projectID,
})
if err != nil {
if ok := helper.IsErrCode(err, codes.NotFound); ok {
tflog.Debug(ctx, fmt.Sprintf("Network %s in project %s is done: ", networkID, projectID))
return res, "done", nil
}

tflog.Error(ctx, fmt.Sprintf("error getting network %s in project %s: %v", networkID, projectID, err))
return nil, "", err
}
if res.Network.ShortState == "" {
tflog.Debug(ctx, fmt.Sprintf("Network %s in project %s is stopped: ", networkID, projectID))
return res, "stop", nil
}

tflog.Trace(ctx, fmt.Sprintf("pending network %s in project %s state: %s", networkID, projectID, res.Network.ShortState))
return res, res.Network.ShortState, nil
}

tflog.Debug(ctx, fmt.Sprintf("waiting for network %s in project %s ", networkID, projectID))

stateConf := &helper.StateChangeConf{
Pending: []string{"fail", "poff", "runn", "stop", "susp", "unde", "boot", "clea", "clon", "dsrz", "epil", "hold", "hotp", "init", "migr", "pend", "prol", "save", "shut", "snap", "unkn"},
Target: []string{"done"},
Refresh: refreshFunc,
Timeout: 2 * time.Hour,
MinTimeout: 3 * time.Second,
}

if _, err := stateConf.WaitForState(ctx); err != nil {
return nil, fmt.Errorf("error waiting for network %s in project %s to be deleted: %w", networkID, projectID, err)
}

return nil, nil
}
13 changes: 6 additions & 7 deletions internal/provider/security_group_data_source.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,12 +137,11 @@ func (d *SecurityGroupDataSource) Read(ctx context.Context, req datasource.ReadR
return
}

sg := res.SecurityGroup
state.ID = types.StringValue(sg.Id)
state.DataCenterID = types.StringValue(sg.DataCenterId)
state.Description = types.StringValue(sg.Description)
state.Rules = getRuleModels(sg.Rules)

// Save data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
resp.Diagnostics.Append(resp.State.Set(ctx, SecurityGroupDataSourceModel{
ID: types.StringValue(res.SecurityGroup.Id),
DataCenterID: types.StringValue(res.SecurityGroup.DataCenterId),
Description: types.StringValue(res.SecurityGroup.Description),
Rules: getRuleModels(res.SecurityGroup.Rules),
})...)
}
16 changes: 7 additions & 9 deletions internal/provider/security_group_resource.go
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ func (r *SecurityGroupResource) Create(ctx context.Context, req resource.CreateR
}

func (r *SecurityGroupResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) {
var state *SecurityGroupResourceModel
var state SecurityGroupResourceModel

// Read Terraform prior state data into the model
resp.Diagnostics.Append(req.State.Get(ctx, &state)...)
Expand All @@ -304,14 +304,13 @@ func (r *SecurityGroupResource) Read(ctx context.Context, req resource.ReadReque
return
}

state.Id = types.StringValue(res.SecurityGroup.Id)
state.Description = types.StringValue(res.SecurityGroup.Description)
state.DataCenterID = types.StringValue(res.SecurityGroup.DataCenterId)
state.Id = types.StringValue(res.SecurityGroup.Id)
state.Rules = getRuleModels(res.SecurityGroup.Rules)

// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
resp.Diagnostics.Append(resp.State.Set(ctx, SecurityGroupResourceModel{
Id: types.StringValue(res.SecurityGroup.Id),
Description: types.StringValue(res.SecurityGroup.Description),
DataCenterID: types.StringValue(res.SecurityGroup.DataCenterId),
Rules: getRuleModels(res.SecurityGroup.Rules),
})...)
}

func (r *SecurityGroupResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) {
Expand Down Expand Up @@ -342,7 +341,6 @@ func (r *SecurityGroupResource) Update(ctx context.Context, req resource.UpdateR
}

state.Rules = getRuleModels(res.SecurityGroup.Rules)

// Save updated data into Terraform state
resp.Diagnostics.Append(resp.State.Set(ctx, &state)...)
}
Expand Down
Loading

0 comments on commit 48f771a

Please sign in to comment.