Skip to content

Commit

Permalink
release v0.1.16
Browse files Browse the repository at this point in the history
  • Loading branch information
contabo committed Oct 21, 2022
1 parent 1950ca9 commit d057e15
Show file tree
Hide file tree
Showing 12 changed files with 164 additions and 47 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ openapi/
terraform-provider-contabo*
examples/main.tf
examples/*.tfstate
examples/*.tfstate.backup
examples/*.tfstate.backup
__debug_bin
2 changes: 0 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,6 @@ build-only-debug:
go mod download
go build -gcflags="all=-N -l" -o terraform-provider-contabo_$(VERSION)



.PHONY: doc-preview
doc-preview:
@echo "Preview your markdown documentation on this page: https://registry.terraform.io/tools/doc-preview"
111 changes: 85 additions & 26 deletions contabo/resource_instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func resourceInstance() *schema.Resource {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Image Id is used to set up the compute instance. Ubuntu 20.04 is the default, currently you have to get the Id with our [API](https://api.contabo.com/#tag/Images/operation/retrieveImage) or via our [command line](https://github.com/contabo/cntb) tool with this command: `cntb get images`.",
Description: "CAUTION: On updating this value your server will be reinstalled! Image Id is used to set up the compute instance. Ubuntu 20.04 is the default, currently you have to get the Id with our [API](https://api.contabo.com/#tag/Images/operation/retrieveImage) or via our [command line](https://github.com/contabo/cntb) tool with this command: `cntb get images`.",
},
"region": {
Type: schema.TypeString,
Expand Down Expand Up @@ -147,12 +147,12 @@ func resourceInstance() *schema.Resource {
Elem: &schema.Schema{
Type: schema.TypeInt,
},
Description: "Array of `secretIds` of public SSH keys for logging into as defaultUser with administrator/root privileges. Applies to Linux/BSD systems. Please refer to Secrets Management API.",
Description: "CAUTION: On updating this value your server will be reinstalled! Array of `secretIds` of public SSH keys for logging into as defaultUser with administrator/root privileges. Applies to Linux/BSD systems. Please refer to Secrets Management API.",
},
"root_password": {
Optional: true,
Type: schema.TypeInt,
Description: "Root password of the compute instance.",
Description: "CAUTION: On updating this value your server will be reinstalled! Root password of the compute instance.",
},
"created_date": {
Type: schema.TypeString,
Expand Down Expand Up @@ -208,7 +208,7 @@ func resourceInstance() *schema.Resource {
"user_data": {
Type: schema.TypeString,
Optional: true,
Description: "Cloud-Init Config in order to customize during start of compute instance.",
Description: "CAUTION: On updating this value your server will be reinstalled! Cloud-Init Config in order to customize during start of compute instance.",
},
"license": {
Type: schema.TypeString,
Expand Down Expand Up @@ -347,13 +347,82 @@ func resourceInstanceRead(ctx context.Context, d *schema.ResourceData, m interfa
func resourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client := m.(*openapi.APIClient)
anyChange := false
instanceId, err := strconv.ParseInt(d.Id(), 10, 64)

if err != nil {
return diag.FromErr(err)
}
if shouldUpdateInstanceValues(d) {
updateInstanceValues(d, client, ctx, instanceId, diags, m)
}
if shouldReinstall(d) {
reinstall(d, client, ctx, instanceId, diags, m)
}
return resourceInstanceRead(ctx, d, m)
}

func shouldUpdateInstanceValues(d *schema.ResourceData) bool {
updateChange := false
if d.HasChange("display_name") {
displayname := d.Get("display_name").(string)
if displayname != "" {
updateChange = true
}
}
return updateChange
}

func shouldReinstall(d *schema.ResourceData) bool {
reinstallChange := false
if d.HasChange("ssh_keys") {
sshKeys := d.Get("ssh_keys")
if sshKeys != nil {
reinstallChange = true
}
}

if d.HasChange("root_password") {
rootPassword := d.Get("root_password")
if rootPassword != nil {
reinstallChange = true
}
}

if d.HasChange("user_data") {
userData := d.Get("user_data").(string)
if userData != "" {
reinstallChange = true
}
}
if d.HasChange("image_id") {
imageId := d.Get("image_id").(string)
if imageId != "" {
reinstallChange = true
}
}
return reinstallChange
}

func updateInstanceValues(d *schema.ResourceData, client *openapi.APIClient, ctx context.Context, instanceId int64, diags diag.Diagnostics, m interface{}) diag.Diagnostics {

patchInstanceRequest := *openapi.NewPatchInstanceRequestWithDefaults()
displayName := d.Get("display_name").(string)
patchInstanceRequest.DisplayName = &displayName

res, httpResp, err := client.InstancesApi.
PatchInstance(context.Background(), instanceId).
PatchInstanceRequest(patchInstanceRequest).
XRequestId(uuid.NewV4().String()).Execute()

if err != nil {
return HandleResponseErrors(diags, httpResp)
} else if len(res.Data) != 1 {
return MultipleDataObjectsError(diags)
}
d.SetId(strconv.Itoa(int(res.Data[0].InstanceId)))
return diags
}

func reinstall(d *schema.ResourceData, client *openapi.APIClient, ctx context.Context, instanceId int64, diags diag.Diagnostics, m interface{}) diag.Diagnostics {
patchInstanceRequest := openapi.NewReinstallInstanceRequestWithDefaults()

if d.HasChange("ssh_keys") {
Expand All @@ -365,7 +434,6 @@ func resourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, m inter
sshKeys64 = append(sshKeys64, int64(sshKey))
}
patchInstanceRequest.SshKeys = &sshKeys64
anyChange = true
}
}

Expand All @@ -374,42 +442,33 @@ func resourceInstanceUpdate(ctx context.Context, d *schema.ResourceData, m inter
if rootPassword != nil {
rootPassword64 := int64(rootPassword.(int))
patchInstanceRequest.RootPassword = &rootPassword64
anyChange = true
}
}

if d.HasChange("user_data") {
userData := d.Get("user_data").(string)
if userData != "" {
patchInstanceRequest.UserData = &userData
anyChange = true
}
}

imageId := d.Get("image_id").(string)
if imageId != "" {
patchInstanceRequest.ImageId = imageId
anyChange = true
}

if anyChange {
res, httpResp, err := client.InstancesApi.
ReinstallInstance(ctx, instanceId).
XRequestId(uuid.NewV4().String()).
ReinstallInstanceRequest(*patchInstanceRequest).
Execute()

if err != nil {
return HandleResponseErrors(diags, httpResp)
} else if len(res.Data) != 1 {
return MultipleDataObjectsError(diags)
}

d.SetId(strconv.Itoa(int(res.Data[0].InstanceId)))
res, httpResp, err := client.InstancesApi.
ReinstallInstance(ctx, instanceId).
XRequestId(uuid.NewV4().String()).
ReinstallInstanceRequest(*patchInstanceRequest).
Execute()

return resourceInstanceRead(ctx, d, m)
if err != nil {
return HandleResponseErrors(diags, httpResp)
} else if len(res.Data) != 1 {
return MultipleDataObjectsError(diags)
}

d.SetId(strconv.Itoa(int(res.Data[0].InstanceId)))
return diags
}

Expand Down
73 changes: 66 additions & 7 deletions contabo/resource_instance_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,25 +15,80 @@ func TestAccContaboInstanceBasic(t *testing.T) {
CheckDestroy: testAccCheckInstanceDestroy,
Steps: []resource.TestStep{
{
Config: testCheckContaboInstanceConfigBasic(),
Config: updateAndReinstallVPSCreation(),
Check: resource.ComposeTestCheckFunc(
testCheckContaboInstanceExists("contabo_instance.new"),
testCheckContaboInstanceExists("contabo_instance.update_reinstall_test"),
resource.TestCheckResourceAttr("contabo_instance.update_reinstall_test", "display_name", "created_display_name"),
),
PreventPostDestroyRefresh: true,
},
{
Config: updateAndReinstallInstallFedora(),
Check: resource.ComposeTestCheckFunc(
testCheckContaboInstanceExists("contabo_instance.update_reinstall_test"),
resource.TestCheckResourceAttr("contabo_instance.update_reinstall_test", "image_id", "1e1802ac-843c-42ed-9533-add37aaff46b"),
),
PreventPostDestroyRefresh: true,
},
{
Config: updateAndReinstallDisplayNameUpdate(),
Check: resource.ComposeTestCheckFunc(
testCheckContaboInstanceExists("contabo_instance.update_reinstall_test"),
resource.TestCheckResourceAttr("contabo_instance.update_reinstall_test", "display_name", "first_updated_display_name"),
),
PreventPostDestroyRefresh: true,
},
{
Config: updateAndReinstallUpdateDisplayNameAndInstallArch(),
Check: resource.ComposeTestCheckFunc(
testCheckContaboInstanceExists("contabo_instance.update_reinstall_test"),
resource.TestCheckResourceAttr("contabo_instance.update_reinstall_test", "display_name", "secound_updated_display_name"),
resource.TestCheckResourceAttr("contabo_instance.update_reinstall_test", "image_id", "69b52ee3-2fda-4f44-b8de-69e480d87c7d"),
),
PreventPostDestroyRefresh: true,
},
},
})
}

func testAccCheckInstanceDestroy(s *terraform.State) error {
return nil
func updateAndReinstallVPSCreation() string {
return `
provider "contabo" {}
resource "contabo_instance" "update_reinstall_test" {
display_name = "created_display_name"
image_id = "66abf39a-ba8b-425e-a385-8eb347ceac10"
}
`
}

func testCheckContaboInstanceConfigBasic() string {
func updateAndReinstallDisplayNameUpdate() string {
return `
provider "contabo" {}
resource "contabo_instance" "new" {
display_name = "custom terraform"
resource "contabo_instance" "update_reinstall_test" {
display_name = "first_updated_display_name"
}
`
}

func updateAndReinstallInstallFedora() string {
return `
provider "contabo" {}
resource "contabo_instance" "update_reinstall_test" {
image_id = "1e1802ac-843c-42ed-9533-add37aaff46b"
}
`
}

func updateAndReinstallUpdateDisplayNameAndInstallArch() string {
return `
provider "contabo" {}
resource "contabo_instance" "update_reinstall_test" {
image_id = "69b52ee3-2fda-4f44-b8de-69e480d87c7d"
display_name = "secound_updated_display_name"
}
`
}
Expand All @@ -53,3 +108,7 @@ func testCheckContaboInstanceExists(n string) resource.TestCheckFunc {
return nil
}
}

func testAccCheckInstanceDestroy(s *terraform.State) error {
return nil
}
2 changes: 1 addition & 1 deletion docs/guides/custom_instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ terraform {
required_providers {
contabo = {
source = "contabo/contabo"
version = "v0.1.15"
version = ">= 0.1.16"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion docs/guides/use_environment_variables.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ terraform {
required_providers {
contabo = {
source = "contabo/contabo"
version = "v0.1.15"
version = ">= 0.1.16"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ terraform {
required_providers {
contabo = {
source = "contabo/contabo"
version = "v0.1.15"
version = ">= 0.1.16"
}
}
}
Expand Down
8 changes: 4 additions & 4 deletions docs/resources/contabo_instance.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ resource "contabo_instance" "database_instance" {
- `add_ons` (Block List) (see [below for nested schema](#nestedblock--add_ons))
- `cancel_date` (String) The date on which the instance will be cancelled.
- `display_name` (String) The instance name chosen by the customer that will be shown in the customer panel.
- `image_id` (String) Image Id is used to set up the compute instance. Ubuntu 20.04 is the default, currently you have to get the Id with our [API](https://api.contabo.com/#tag/Images/operation/retrieveImage) or via our [command line](https://github.com/contabo/cntb) tool with this command: `cntb get images`.
- `image_id` (String) CAUTION: On updating this value your server will be reinstalled! Image Id is used to set up the compute instance. Ubuntu 20.04 is the default, currently you have to get the Id with our [API](https://api.contabo.com/#tag/Images/operation/retrieveImage) or via our [command line](https://github.com/contabo/cntb) tool with this command: `cntb get images`.
- `license` (String) Additional license in order to enhance your chosen product. It is mainly needed for software licenses on your product (not needed for windows). See our [api documentation](https://api.contabo.com/#tag/Instances/operation/createInstance) for all available licenses.
- `period` (Number) Initial contract period in months. Available periods are: 1, 3, 6 and 12 months. The default setting is 1 month.
- `product_id` (String) Choose the VPS/VDS product you want to buy. See our products [here](https://api.contabo.com/#tag/Instances/operation/createInstance).
- `region` (String) Instance Region where the compute instance should be located. Default region is the EU. Following regions are available: `EU`,`US-central`,`US-east`,`US-west`,`SIN`.
- `root_password` (Number) Root password of the compute instance.
- `ssh_keys` (List of Number) Array of `secretIds` of public SSH keys for logging into as defaultUser with administrator/root privileges. Applies to Linux/BSD systems. Please refer to Secrets Management API.
- `user_data` (String) Cloud-Init Config in order to customize during start of compute instance.
- `root_password` (Number) CAUTION: On updating this value your server will be reinstalled! Root password of the compute instance.
- `ssh_keys` (List of Number) CAUTION: On updating this value your server will be reinstalled! Array of `secretIds` of public SSH keys for logging into as defaultUser with administrator/root privileges. Applies to Linux/BSD systems. Please refer to Secrets Management API.
- `user_data` (String) CAUTION: On updating this value your server will be reinstalled! Cloud-Init Config in order to customize during start of compute instance.

### Read-Only

Expand Down
2 changes: 1 addition & 1 deletion examples/custom_instance/custom_instance.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ terraform {
required_providers {
contabo = {
source = "contabo/contabo"
version = "v0.1.15"
version = ">= 0.1.16"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/main.tf.example
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ terraform {
required_providers {
contabo = {
source = "contabo/contabo"
version = "v0.1.15"
version = ">= 0.1.16"
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion examples/provider/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ terraform {
required_providers {
contabo = {
source = "contabo/contabo"
version = "v0.1.15"
version = ">= 0.1.16"
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ terraform {
required_providers {
contabo = {
source = "contabo/contabo"
version = "v0.1.15"
version = ">= 0.1.16"
}
}
}
Expand Down

0 comments on commit d057e15

Please sign in to comment.