Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
248 changes: 248 additions & 0 deletions cancom/services/ipam/migration_v1.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,248 @@
package ipam

import (
"context"
"fmt"
"strconv"
"time"

"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)

var resourceSchemaInstanceV0 = &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"name_tag": {
Type: schema.TypeString,
Computed: false,
Required: true,
},
"managed_by": {
Type: schema.TypeString,
Computed: false,
Required: true,
},
"description": {
Type: schema.TypeString,
Computed: false,
Optional: true,
},
"release_wait_time": {
Type: schema.TypeString,
Optional: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
},
},
}

func instanceUpgradeV0() schema.StateUpgrader {
return schema.StateUpgrader{
Type: resourceSchemaInstanceV0.CoreConfigSchema().ImpliedType(),
Version: 0,
Upgrade: func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
if val, ok := rawState["updated_at"].(string); ok {
if val != "" {
i, err := time.Parse("2006-01-02T15:04:05", val)
if err != nil {
return rawState, fmt.Errorf("error converting updated_at to int: %w", err)
}
rawState["updated_at"] = int(i.Unix())
} else {
rawState["updated_at"] = nil
}
}

if val, ok := rawState["created_at"].(string); ok {
if val != "" {
i, err := time.Parse("2006-01-02T15:04:05", val)
if err != nil {
return rawState, fmt.Errorf("error converting created_at to int: %w", err)
}
rawState["created_at"] = int(i.Unix())
} else {
rawState["created_at"] = nil
}
}

if val, ok := rawState["release_wait_time"].(string); ok {
i, err := strconv.Atoi(val)
if err != nil {
return rawState, fmt.Errorf("failed to convert release_wait_time from string to int: %w", err)
}
rawState["release_wait_time"] = i
}

return rawState, nil
},
}
}

var resourceSchemaNetworkV0 = &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"supernet_id": {
Type: schema.TypeString,
Computed: false,
Required: true,
ForceNew: true,
},
"name_tag": {
Type: schema.TypeString,
Computed: false,
Required: true,
},
"description": {
Type: schema.TypeString,
Computed: false,
Optional: true,
},
"request": {
Type: schema.TypeString,
Computed: false,
Required: true,
ForceNew: true,
},
"host_assign": {
Type: schema.TypeBool,
Computed: false,
Optional: true,
Default: true,
},
"prefix_str": {
Type: schema.TypeString,
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
},
},
}

func networkUpgradeV0() schema.StateUpgrader {
return schema.StateUpgrader{
Type: resourceSchemaNetworkV0.CoreConfigSchema().ImpliedType(),
Version: 0,
Upgrade: func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
if val, ok := rawState["updated_at"].(string); ok {
if val != "" {
i, err := time.Parse("2006-01-02T15:04:05", val)
if err != nil {
return rawState, fmt.Errorf("error converting updated_at to int: %w", err)
}
rawState["updated_at"] = int(i.Unix())
} else {
rawState["updated_at"] = nil
}
}

if val, ok := rawState["created_at"].(string); ok {
if val != "" {
i, err := time.Parse("2006-01-02T15:04:05", val)
if err != nil {
return rawState, fmt.Errorf("error converting created_at to int: %w", err)
}
rawState["created_at"] = int(i.Unix())
} else {
rawState["created_at"] = nil
}
}

return rawState, nil
},
}
}

var resourceSchemaSupernetV0 = &schema.Resource{
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Computed: true,
},
"instance_id": {
Type: schema.TypeString,
Computed: false,
Required: true,
},
"name_tag": {
Type: schema.TypeString,
Computed: false,
Required: true,
},
"description": {
Type: schema.TypeString,
Computed: false,
Optional: true,
},
"supernet_cidr": {
Type: schema.TypeString,
Computed: false,
Required: true,
ForceNew: true,
},
"created_at": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
"updated_at": {
Type: schema.TypeString,
Computed: true,
Optional: true,
},
},
}

func supernetUpgradeV0() schema.StateUpgrader {
return schema.StateUpgrader{

Type: resourceSchemaSupernetV0.CoreConfigSchema().ImpliedType(),
Version: 0,
Upgrade: func(ctx context.Context, rawState map[string]interface{}, meta interface{}) (map[string]interface{}, error) {
if val, ok := rawState["updated_at"].(string); ok {
if val != "" {
i, err := time.Parse("2006-01-02T15:04:05", val)
if err != nil {
return rawState, fmt.Errorf("error converting updated_at to int: %w", err)
}
rawState["updated_at"] = int(i.Unix())
} else {

rawState["updated_at"] = 0
}

}

if val, ok := rawState["created_at"].(string); ok {
if val != "" {
i, err := time.Parse("2006-01-02T15:04:05", val)
if err != nil {
return rawState, fmt.Errorf("error converting created_at to int: %w", err)
}
rawState["created_at"] = int(i.Unix())
} else {
rawState["created_at"] = nil
}
}

return rawState, nil
},
}
}
2 changes: 1 addition & 1 deletion cancom/services/ipam/resource_host.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ func resourceHostCreate(ctx context.Context, d *schema.ResourceData, m interface
host := &client_ipam.HostCreateRequest{
NetworkCrn: d.Get("network_crn").(string),
NameTag: d.Get("name_tag").(string),
Operation: "assign_address",
Operation: "assignAddress",
Description: d.Get("description").(string),
Qualifier: d.Get("qualifier").(string),
}
Expand Down
15 changes: 10 additions & 5 deletions cancom/services/ipam/resource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func resourceInstance() *schema.Resource {
ReadContext: resourceInstanceRead,
UpdateContext: resourceInstanceUpdate,
DeleteContext: resourceInstanceDelete,
SchemaVersion: 1,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Expand All @@ -37,18 +38,22 @@ func resourceInstance() *schema.Resource {
Optional: true,
},
"release_wait_time": {
Type: schema.TypeString,
Type: schema.TypeInt,
Default: 0,
Optional: true,
},
"created_at": {
Type: schema.TypeString,
Type: schema.TypeInt,
Computed: true,
},
"updated_at": {
Type: schema.TypeString,
Type: schema.TypeInt,
Computed: true,
},
},
StateUpgraders: []schema.StateUpgrader{
instanceUpgradeV0(),
},
}
}

Expand Down Expand Up @@ -90,7 +95,7 @@ func resourceInstanceCreate(ctx context.Context, d *schema.ResourceData, m inter
instance := &client_ipam.InstanceCreateRequest{
NameTag: d.Get("name_tag").(string),
ManagedBy: d.Get("managed_by").(string),
ReleaseWaitTime: d.Get("release_wait_time").(string),
ReleaseWaitTime: d.Get("release_wait_time").(int),
Description: d.Get("description").(string),
}

Expand Down Expand Up @@ -123,7 +128,7 @@ func resourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, m inter
instance := &client_ipam.InstanceUpdateRequest{
NameTag: d.Get("name_tag").(string),
ManagedBy: d.Get("managed_by").(string),
ReleaseWaitTime: d.Get("release_wait_time").(string),
ReleaseWaitTime: d.Get("release_wait_time").(int),
Description: d.Get("description").(string),
}

Expand Down
8 changes: 6 additions & 2 deletions cancom/services/ipam/resource_network.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func resourceNetwork() *schema.Resource {
ReadContext: resourceNetworkRead,
UpdateContext: resourceNetworkUpdate,
DeleteContext: resourceNetworkDelete,
SchemaVersion: 1,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -54,14 +55,17 @@ func resourceNetwork() *schema.Resource {
Computed: true,
},
"created_at": {
Type: schema.TypeString,
Type: schema.TypeInt,
Computed: true,
},
"updated_at": {
Type: schema.TypeString,
Type: schema.TypeInt,
Computed: true,
},
},
StateUpgraders: []schema.StateUpgrader{
networkUpgradeV0(),
},
}
}

Expand Down
8 changes: 6 additions & 2 deletions cancom/services/ipam/resource_supernet.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ func resourceSupernet() *schema.Resource {
ReadContext: resourceSupernetRead,
UpdateContext: resourceSupernetUpdate,
DeleteContext: resourceSupernetDelete,
SchemaVersion: 1,
Schema: map[string]*schema.Schema{
"id": {
Type: schema.TypeString,
Expand Down Expand Up @@ -43,14 +44,17 @@ func resourceSupernet() *schema.Resource {
ForceNew: true,
},
"created_at": {
Type: schema.TypeString,
Type: schema.TypeInt,
Computed: true,
},
"updated_at": {
Type: schema.TypeString,
Type: schema.TypeInt,
Computed: true,
},
},
StateUpgraders: []schema.StateUpgrader{
supernetUpgradeV0(),
},
}
}

Expand Down
2 changes: 1 addition & 1 deletion client/services/ipam/ipam.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,7 +431,7 @@ func (c *Client) DeleteHost(hostId string) error {
return err
}
// included to find a sporadic problem leading to resources not released in the backend
if delResponse.Message != "released successfully" {
if delResponse.Message != "Item released successfully" && delResponse.Message != "released successfully" {
return errors.New("message: " + delResponse.Message)
}

Expand Down
Loading