Skip to content

Commit

Permalink
Allow to set user_data in server resource (#231)
Browse files Browse the repository at this point in the history
* allow to set user_data in server rs

* update server rs/ds docs

* rename user_data to user_data_base64

* allow plain text user_data

if both `user_data` and `user_data_base64`
are set, `user_data` will be ignored.

* Revert "allow plain text user_data"

This reverts commit 1973415.

* prevent malformed base64 user_data
  • Loading branch information
nvthongswansea committed May 19, 2023
1 parent 2af157e commit fcea972
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 1 deletion.
9 changes: 9 additions & 0 deletions gridscale/datasource_gridscale_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,11 @@ func dataSourceGridscaleServer() *schema.Resource {
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"user_data_base64": {
Type: schema.TypeString,
Description: "For system configuration on first boot. May contain cloud-config data or shell scripting, encoded as base64 string. Supported tools are cloud-init, Cloudbase-init, and Ignition.",
Computed: true,
},
},
}
}
Expand Down Expand Up @@ -362,6 +367,10 @@ func dataSourceGridscaleServerRead(d *schema.ResourceData, meta interface{}) err
return fmt.Errorf("%s error setting labels: %v", errorPrefix, err)
}

if err = d.Set("user_data_base64", server.Properties.UserData); err != nil {
return fmt.Errorf("%s error setting user_data_base64: %v", errorPrefix, err)
}

//Get storages
storages := make([]interface{}, 0)
for _, value := range server.Properties.Relations.Storages {
Expand Down
2 changes: 1 addition & 1 deletion gridscale/relation-manager/serverRelationManager.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,7 +235,7 @@ func (c *ServerRelationManger) IsShutdownRequired(ctx context.Context) bool {
var shutdownRequired bool
d := c.getData()
hasServerNetListChange := c.hasServerNetworkListChanged(ctx)
if d.HasChanges("cores", "memory", "ipv4", "ipv6", "hardware_profile", "hardware_profile_config", "auto_recovery") || hasServerNetListChange {
if d.HasChanges("cores", "memory", "ipv4", "ipv6", "hardware_profile", "hardware_profile_config", "auto_recovery", "user_data_base64") || hasServerNetListChange {
shutdownRequired = true
}
return shutdownRequired
Expand Down
29 changes: 29 additions & 0 deletions gridscale/resource_gridscale_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package gridscale

import (
"context"
"encoding/base64"
"fmt"
"log"
"net/http"
Expand Down Expand Up @@ -397,6 +398,12 @@ func resourceGridscaleServer() *schema.Resource {
Optional: true,
Elem: &schema.Schema{Type: schema.TypeString},
},
"user_data_base64": {
Type: schema.TypeString,
Description: "For system configuration on first boot. May contain cloud-config data or shell scripting, encoded as base64 string. Supported tools are cloud-init, Cloudbase-init, and Ignition.",
Optional: true,
Computed: true,
},
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(5 * time.Minute),
Expand Down Expand Up @@ -560,6 +567,10 @@ func resourceGridscaleServerRead(d *schema.ResourceData, meta interface{}) error
return fmt.Errorf("%s error setting labels: %v", errorPrefix, err)
}

if err = d.Set("user_data_base64", server.Properties.UserData); err != nil {
return fmt.Errorf("%s error setting user_data_base64: %v", errorPrefix, err)
}

//Get storages
storages := make([]interface{}, 0)
for _, value := range server.Properties.Relations.Storages {
Expand Down Expand Up @@ -756,6 +767,15 @@ func resourceGridscaleServerCreate(d *schema.ResourceData, meta interface{}) err
requestBody.AutoRecovery = autoRecovery
}

if val, ok := d.GetOk("user_data_base64"); ok {
userData := val.(string)
// check if user_data is base64 encoded
if _, err := base64.StdEncoding.DecodeString(userData); err != nil {
return fmt.Errorf("user_data_base64 is not base64 encoded")
}
requestBody.UserData = &userData
}

profile := d.Get("hardware_profile").(string)
switch profile {
case "legacy":
Expand Down Expand Up @@ -1009,6 +1029,15 @@ func resourceGridscaleServerUpdate(d *schema.ResourceData, meta interface{}) err
requestBody.AutoRecovery = &autoRecovery
}

if val, ok := d.GetOk("user_data_base64"); ok {
userData := val.(string)
// check if user_data is base64 encoded
if _, err := base64.StdEncoding.DecodeString(userData); err != nil {
return fmt.Errorf("user_data_base64 is not base64 encoded")
}
requestBody.UserData = &userData
}

updateSequence := func(ctx context.Context) error {
//Execute the update request
err = gsc.UpdateServer(ctx, d.Id(), requestBody)
Expand Down
1 change: 1 addition & 0 deletions website/docs/d/server.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ This resource exports the following attributes:
* `location_uuid` - The location this server is placed. The location of a resource is determined by it's project.
* `labels` - List of labels in the format [ "label1", "label2" ].
* `hardware_profile` - The hardware profile of the server.
* `user_data_base64` - For system configuration on first boot. May contain cloud-config data or shell scripting, encoded as base64 string. Supported tools are cloud-init, Cloudbase-init, and Ignition.
* `hardware_profile_config` - Specifies the custom hardware settings for the virtual machine. Note: hardware_profile and hardware_profile_config parameters can't be used at the same time.
* `machinetype` - Values: `"i440fx"`, `"q35_bios"`, `"q35_uefi"`.
* `storage_device` - Values: `"ide"`, `"sata"`, `"virtio_scsi"`, `"virtio_block"`.
Expand Down
3 changes: 3 additions & 0 deletions website/docs/r/server.html.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ The following arguments are supported:

* `auto_recovery` - (Optional) If the server should be auto-started in case of a failure (default=true).

* `user_data_base64` - (Optional, Computed) For system configuration on first boot. May contain cloud-config data or shell scripting, encoded as base64 string. Supported tools are cloud-init, Cloudbase-init, and Ignition.

* `hardware_profile` - (Optional, Computed) The hardware profile of the Server. Options are default, legacy, nested, cisco_csr, sophos_utm, f5_bigip and q35 at the moment of writing. If it is not set, the backend will set it by default. Check [the official docs](https://gridscale.io/en/api-documentation/index.html#operation/createServer).

* `ipv4` - (Optional) The UUID of the IPv4 address of the server. (***NOTE: The server will NOT automatically be connected to the public network; to give it access to the internet, please add server to the public network.)
Expand Down Expand Up @@ -229,6 +231,7 @@ This resource exports the following attributes:
* `location_uuid` - The location this server is placed. The location of a resource is determined by it's project.
* `labels` - List of labels in the format [ "label1", "label2" ].
* `hardware_profile` - The hardware profile of the server.
* `user_data_base64` - See Argument Reference above.
* `hardware_profile_config` - (See Argument Reference above.
* `machinetype` - See Argument Reference above.
* `storage_device` - See Argument Reference above.
Expand Down

0 comments on commit fcea972

Please sign in to comment.