Skip to content

Commit

Permalink
feature: add hostname to server and vcpu_server, cube_server, and ser…
Browse files Browse the repository at this point in the history
…vers ds (#697)
  • Loading branch information
cristiGuranIonos authored Oct 28, 2024
1 parent 74218de commit bac7fc9
Show file tree
Hide file tree
Showing 22 changed files with 174 additions and 62 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
## 6.6.1

### Features
- Add `hostname` to `ionoscloud_server` resource and data source
- Add `hostname` to `ionoscloud_vcpu_server` resource and data source
- Add `hostname` to `ionoscloud_cube_server` resource and data source

## 6.6.0
### Refactor
- Rename `S3` occurrences to `Object Storage`
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/cube_server.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ The following attributes are returned by the datasource:
* `template_uuid` - The UUID of the template for creating a CUBE server; the available templates for CUBE servers can be found on the templates resource
* `id` - The id of that resource
* `name` - The name of that resource
* `hostname` - The hostname of the server
* `type` - Server usages: CUBE
* `vm_state`- Status of the virtual Machine
* `datacenter_id` - The id of the datacenter
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ data "ionoscloud_server" "example" {
The following attributes are returned by the datasource:

* `template_uuid` - The UUID of the template for creating a CUBE server; the available templates for CUBE servers can be found on the templates resource
* `hostname` - The hostname of the resource.
* `id` - The id of that resource
* `name` - The name of that resource
* `type` - Server usages: [ENTERPRISE](https://docs.ionos.com/cloud/compute-engine/virtual-servers/virtual-servers) or [CUBE](https://docs.ionos.com/cloud/compute-engine/virtual-servers/cloud-cubes)
Expand Down
1 change: 1 addition & 0 deletions docs/data-sources/vcpu_server.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ The following attributes are returned by the datasource:

* `id` - The id of that resource
* `name` - The name of that resource
* `hostname` - The hostname of the server
* `type` - The type of the server, should always be `VCPU`
* `vm_state`- Status of the virtual Machine
* `datacenter_id` - The id of the datacenter
Expand Down
5 changes: 3 additions & 2 deletions docs/resources/cube_server.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ This resource will create an operational server. After this section completes, t

```hcl
data "ionoscloud_template" "example" {
name = "CUBES XS"
name = "Basic Cube XS"
}
resource "ionoscloud_datacenter" "example" {
Expand Down Expand Up @@ -62,7 +62,7 @@ resource "random_password" "server_image_password" {

```hcl
data "ionoscloud_template" "example" {
name = "CUBES XS"
name = "Basic Cube XS"
}
resource "ionoscloud_datacenter" "example" {
name = "Datacenter Example"
Expand Down Expand Up @@ -120,6 +120,7 @@ resource "random_password" "server_image_password" {
- `template_uuid` - (Required)[string] The UUID of the template for creating a CUBE server; the available templates for CUBE servers can be found on the templates resource
- `name` - (Required)[string] The name of the server.
- `datacenter_id` - (Required)[string] The ID of a Virtual Data Center.
- `hostname` - The hostname of the resource. Allowed characters are a-z, 0-9 and - (minus). Hostname should not start with minus and should not be longer than 63 characters.
- `image_name` - (Optional)[string] The name, ID or alias of the image. May also be a snapshot ID. It is required if `licence_type` is not provided. Attribute is immutable.
- `availability_zone` - (Optional)[string] The availability zone in which the server should exist. This property is immutable.
- `licence_type` - (Optional)[string] Sets the OS type of the server.
Expand Down
3 changes: 2 additions & 1 deletion docs/resources/server.md
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ resource "random_password" "server_image_password" {

```hcl
data "ionoscloud_template" "example" {
name = "CUBES XS"
name = "Basic Cube XS"
}
resource "ionoscloud_datacenter" "example" {
Expand Down Expand Up @@ -261,6 +261,7 @@ resource "ionoscloud_server" "test" {
- `template_uuid` - (Optional)[string] The UUID of the template for creating a CUBE server; the available templates for CUBE servers can be found on the templates resource
- `name` - (Required)[string] The name of the server.
- `datacenter_id` - (Required)[string] The ID of a Virtual Data Center.
- `hostname` - (Optional)[string] The hostname of the resource. Allowed characters are a-z, 0-9 and - (minus). Hostname should not start with minus and should not be longer than 63 characters.
- `cores` - (Optional)(Computed)[integer] Number of server CPU cores.
- `ram` - (Optional)(Computed)[integer] The amount of memory for the server in MB.
- `image_name` - (Optional)[string] The name, ID or alias of the image. May also be a snapshot ID. It is required if `licence_type` is not provided. Attribute is immutable.
Expand Down
1 change: 1 addition & 0 deletions docs/resources/vcpu_server.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ resource "random_password" "server_image_password" {

- `name` - (Required)[string] The name of the server.
- `datacenter_id` - (Required)[string] The ID of a Virtual Data Center.
- `hostname` - (Optional)[string] The hostname of the resource. Allowed characters are a-z, 0-9 and - (minus). Hostname should not start with minus and should not be longer than 63 characters.
- `cores` - (Optional)[integer] Number of server CPU cores.
- `ram` - (Optional)[integer] The amount of memory for the server in MB.
- `image_name` - (Optional)[string] The name, ID or alias of the image. May also be a snapshot ID. It is required if `licence_type` is not provided. Attribute is immutable.
Expand Down
2 changes: 1 addition & 1 deletion docs/resources/volume.md
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ resource "ionoscloud_volume" "example" {
user_data = "foo"
}
resource "ionoscloud_volume" "example" {
resource "ionoscloud_volume" "example2" {
datacenter_id = ionoscloud_datacenter.example.id
server_id = ionoscloud_server.example.id
name = "Another Volume Example"
Expand Down
9 changes: 9 additions & 0 deletions ionoscloud/data_source_cube_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,10 @@ func dataSourceCubeServer() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"hostname": {
Type: schema.TypeString,
Computed: true,
},
"availability_zone": {
Type: schema.TypeString,
Computed: true,
Expand Down Expand Up @@ -201,6 +205,11 @@ func setCubeServerData(d *schema.ResourceData, server *ionoscloud.Server, token
return err
}
}
if server.Properties.Hostname != nil {
if err := d.Set("hostname", *server.Properties.Hostname); err != nil {
return err
}
}
if server.Properties.Cores != nil {
if err := d.Set("cores", *server.Properties.Cores); err != nil {
return err
Expand Down
10 changes: 9 additions & 1 deletion ionoscloud/data_source_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ func dataSourceServer() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"hostname": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -240,7 +244,11 @@ func setServerData(d *schema.ResourceData, server *ionoscloud.Server, token *ion
return err
}
}

if server.Properties.Hostname != nil {
if err := d.Set("hostname", *server.Properties.Hostname); err != nil {
return err
}
}
if server.Properties.Cores != nil {
if err := d.Set("cores", *server.Properties.Cores); err != nil {
return err
Expand Down
6 changes: 5 additions & 1 deletion ionoscloud/data_source_servers.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,10 @@ func dataSourceServers() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},

"hostname": {
Type: schema.TypeString,
Computed: true,
},
"cores": {
Type: schema.TypeInt,
Computed: true,
Expand Down Expand Up @@ -368,6 +371,7 @@ func SetServerProperties(server ionoscloud.Server) map[string]interface{} {
if server.Properties != nil {
utils.SetPropWithNilCheck(serverMap, "template_uuid", server.Properties.TemplateUuid)
utils.SetPropWithNilCheck(serverMap, "name", server.Properties.Name)
utils.SetPropWithNilCheck(serverMap, "hostname", server.Properties.Hostname)
utils.SetPropWithNilCheck(serverMap, "cores", server.Properties.Cores)
utils.SetPropWithNilCheck(serverMap, "ram", server.Properties.Ram)
utils.SetPropWithNilCheck(serverMap, "availability_zone", server.Properties.AvailabilityZone)
Expand Down
4 changes: 4 additions & 0 deletions ionoscloud/data_source_vcpu_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,10 @@ func dataSourceVCPUServer() *schema.Resource {
Type: schema.TypeString,
Optional: true,
},
"hostname": {
Type: schema.TypeString,
Computed: true,
},
"type": {
Type: schema.TypeString,
Computed: true,
Expand Down
2 changes: 1 addition & 1 deletion ionoscloud/import_s3_keys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ func TestAccKeyImportBasic(t *testing.T) {
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
ProtoV6ProviderFactories: testAccProtoV6ProviderFactoriesInternal(t, &testAccProvider),
CheckDestroy: testAccChecks3KeyDestroyCheck,
CheckDestroy: testAccChecksKeyDestroyCheck,
Steps: []resource.TestStep{
{
Config: testAccImportS3KeyConfigBasic,
Expand Down
37 changes: 31 additions & 6 deletions ionoscloud/resource_cube_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,12 @@ func resourceCubeServer() *schema.Resource {
Required: true,
ValidateDiagFunc: validation.ToDiagFunc(validation.StringIsNotWhiteSpace),
},
"hostname": {
Type: schema.TypeString,
Optional: true,
Description: "The hostname of the resource. Allowed characters are a-z, 0-9 and - (minus). Hostname should not start with minus and should not be longer than 63 characters.",
ValidateDiagFunc: validation.ToDiagFunc(validation.All(validation.StringIsNotWhiteSpace, validation.StringLenBetween(1, 63))),
},
"availability_zone": {
Type: schema.TypeString,
Optional: true,
Expand Down Expand Up @@ -395,6 +401,13 @@ func resourceCubeServerCreate(ctx context.Context, d *schema.ResourceData, meta
serverType := constant.CubeType
server.Properties.Type = &serverType

if v, ok := d.GetOk("hostname"); ok {
if v.(string) != "" {
vStr := v.(string)
server.Properties.Hostname = &vStr
}
}

if _, ok := d.GetOk("boot_cdrom"); ok {
resId := d.Get("boot_cdrom").(string)
server.Properties.BootCdrom = &ionoscloud.ResourceReference{
Expand Down Expand Up @@ -430,29 +443,24 @@ func resourceCubeServerCreate(ctx context.Context, d *schema.ResourceData, meta
} else {
volume.ImageAlias = nil
}

if backupUnitID, ok := d.GetOk("volume.0.backup_unit_id"); ok {
if utils.IsValidUUID(backupUnitID.(string)) {
if image == "" && imageAlias == "" {
diags := diag.FromErr(fmt.Errorf("it is mandatory to provide either public image or imageAlias in conjunction with backup unit id property"))
return diags
}

backupUnitID := backupUnitID.(string)
volume.BackupunitId = &backupUnitID
}
}

if userData, ok := d.GetOk("volume.0.user_data"); ok {
if image == "" && imageAlias == "" {
diags := diag.FromErr(fmt.Errorf("it is mandatory to provide either public image or imageAlias that has cloud-init compatibility in conjunction with backup unit id property "))
return diags
}

userData := userData.(string)
volume.UserData = &userData
}

server.Entities = &ionoscloud.ServerEntities{
Volumes: &ionoscloud.AttachedVolumes{
Items: &[]ionoscloud.Volume{
Expand Down Expand Up @@ -635,6 +643,13 @@ func resourceCubeServerRead(ctx context.Context, d *schema.ResourceData, meta in
}
}

if server.Properties.Hostname != nil {
if err := d.Set("hostname", *server.Properties.Hostname); err != nil {
diags := diag.FromErr(err)
return diags
}
}

if server.Properties.AvailabilityZone != nil {
if err := d.Set("availability_zone", *server.Properties.AvailabilityZone); err != nil {
diags := diag.FromErr(err)
Expand Down Expand Up @@ -787,6 +802,12 @@ func resourceCubeServerUpdate(ctx context.Context, d *schema.ResourceData, meta
request.Name = &nStr
}

if d.HasChange("hostname") {
_, n := d.GetChange("hostname")
nStr := n.(string)
request.Hostname = &nStr
}

if d.HasChange("boot_cdrom") {
_, n := d.GetChange("boot_cdrom")
bootCdrom := n.(string)
Expand Down Expand Up @@ -1091,7 +1112,11 @@ func resourceCubeServerImport(ctx context.Context, d *schema.ResourceData, meta
return nil, fmt.Errorf("error setting name %w", err)
}
}

if server.Properties.Hostname != nil {
if err := d.Set("hostname", *server.Properties.Hostname); err != nil {
return nil, fmt.Errorf("error setting hostname %w", err)
}
}
if server.Properties.Name != nil {
if err := d.Set("template_uuid", *server.Properties.TemplateUuid); err != nil {
return nil, fmt.Errorf("error setting template uuid %w", err)
Expand Down
Loading

0 comments on commit bac7fc9

Please sign in to comment.