Skip to content

Commit

Permalink
feat: support to build Azure images with additional disks (#66)
Browse files Browse the repository at this point in the history
* Add support to build Azure images with additional disks

* Update license headers

* Update licence header

---------

Co-authored-by: sternik <[email protected]>
  • Loading branch information
sternik and sternik authored May 29, 2024
1 parent f5b0b8e commit a9d1f66
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 30 deletions.
2 changes: 1 addition & 1 deletion docs/resources/custom_component.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ description: |-
## Example Usage

```terraform
// Copyright 2021-2023 Nordcloud Oy or its affiliates. All Rights Reserved.
// Copyright 2021-2024 Nordcloud Oy or its affiliates. All Rights Reserved.
# An example of a SHELL component
Expand Down
31 changes: 27 additions & 4 deletions docs/resources/template.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ description: |-
## Example Usage

```terraform
// Copyright 2021-2023 Nordcloud Oy or its affiliates. All Rights Reserved.
// Copyright 2021-2024 Nordcloud Oy or its affiliates. All Rights Reserved.
resource "imagefactory_custom_component" "build_template" {
name = "Install nginx"
Expand Down Expand Up @@ -45,9 +45,7 @@ resource "imagefactory_custom_component" "test_component" {
}
data "imagefactory_system_component" "hardening-level-1" {
name = "Hardening level 1"
cloud_provider = "AWS"
stage = "BUILD"
name = "Hardening level 1"
}
data "imagefactory_distribution" "ubuntu18" {
Expand Down Expand Up @@ -130,6 +128,22 @@ resource "imagefactory_template" "azure_template" {
}
}
# AZURE Template - additional data disks
resource "imagefactory_template" "azure_template" {
name = "Ubuntu1804"
description = "Ubuntu 18.04 on Azure"
cloud_provider = "AZURE"
distribution_id = data.imagefactory_distribution.ubuntu18.id
config {
azure {
additional_data_disks {
size = 1
}
}
}
}
output "azure_template" {
value = imagefactory_template.azure_template
}
Expand Down Expand Up @@ -293,11 +307,20 @@ Required:

Optional:

- `additional_data_disks` (Block List, Max: 10) (see [below for nested schema](#nestedblock--config--azure--additional_data_disks))
- `eol_date_option` (Boolean) Default value is set to true
- `exclude_from_latest` (Boolean)
- `replica_regions` (List of String)
- `vm_image_definition` (Block List) (see [below for nested schema](#nestedblock--config--azure--vm_image_definition))

<a id="nestedblock--config--azure--additional_data_disks"></a>
### Nested Schema for `config.azure.additional_data_disks`

Required:

- `size` (Number) Data disk size between 1 and 10 GB.


<a id="nestedblock--config--azure--vm_image_definition"></a>
### Nested Schema for `config.azure.vm_image_definition`

Expand Down
22 changes: 18 additions & 4 deletions examples/resources/imagefactory_template/resource.tf
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2023 Nordcloud Oy or its affiliates. All Rights Reserved.
// Copyright 2021-2024 Nordcloud Oy or its affiliates. All Rights Reserved.

resource "imagefactory_custom_component" "build_template" {
name = "Install nginx"
Expand Down Expand Up @@ -30,9 +30,7 @@ resource "imagefactory_custom_component" "test_component" {
}

data "imagefactory_system_component" "hardening-level-1" {
name = "Hardening level 1"
cloud_provider = "AWS"
stage = "BUILD"
name = "Hardening level 1"
}

data "imagefactory_distribution" "ubuntu18" {
Expand Down Expand Up @@ -115,6 +113,22 @@ resource "imagefactory_template" "azure_template" {
}
}

# AZURE Template - additional data disks

resource "imagefactory_template" "azure_template" {
name = "Ubuntu1804"
description = "Ubuntu 18.04 on Azure"
cloud_provider = "AZURE"
distribution_id = data.imagefactory_distribution.ubuntu18.id
config {
azure {
additional_data_disks {
size = 1
}
}
}
}

output "azure_template" {
value = imagefactory_template.azure_template
}
Expand Down
6 changes: 3 additions & 3 deletions imagefactory/imagetemplate/resource.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2022 Nordcloud Oy or its affiliates. All Rights Reserved.
// Copyright 2021-2024 Nordcloud Oy or its affiliates. All Rights Reserved.

package imagetemplate

Expand Down Expand Up @@ -40,7 +40,7 @@ func resourceTemplateCreate(ctx context.Context, d *schema.ResourceData, m inter
Provider: graphql.Provider(d.Get("cloud_provider").(string)),
Config: *tplCfg,
}
if len(d.Get("description").(string)) > 0 {
if d.Get("description").(string) != "" {
description := graphql.String(d.Get("description").(string))
input.Description = &description
}
Expand Down Expand Up @@ -81,7 +81,7 @@ func resourceTemplateUpdate(ctx context.Context, d *schema.ResourceData, m inter
Name: &name,
Config: tplCfg,
}
if len(d.Get("description").(string)) > 0 {
if d.Get("description").(string) != "" {
description := graphql.String(d.Get("description").(string))
input.Description = &description
}
Expand Down
19 changes: 18 additions & 1 deletion imagefactory/imagetemplate/schema.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2023 Nordcloud Oy or its affiliates. All Rights Reserved.
// Copyright 2021-2024 Nordcloud Oy or its affiliates. All Rights Reserved.

package imagetemplate

Expand Down Expand Up @@ -117,6 +117,17 @@ var vmImageDefinitionAzureTemplateConfigResource = &schema.Resource{
},
}

var additionalDataDisksResource = &schema.Resource{
Schema: map[string]*schema.Schema{
"size": {
Type: schema.TypeInt,
Required: true,
Description: "Data disk size between 1 and 10 GB.",
ValidateFunc: validation.IntBetween(1, 10), // nolint: gomnd
},
},
}

var azureTemplateConfigResource = &schema.Resource{
Schema: map[string]*schema.Schema{
"exclude_from_latest": {
Expand All @@ -141,6 +152,12 @@ var azureTemplateConfigResource = &schema.Resource{
Optional: true,
Elem: vmImageDefinitionAzureTemplateConfigResource,
},
"additional_data_disks": {
Type: schema.TypeList,
Optional: true,
Elem: additionalDataDisksResource,
MaxItems: 10,
},
},
}

Expand Down
18 changes: 17 additions & 1 deletion imagefactory/imagetemplate/structures.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021-2023 Nordcloud Oy or its affiliates. All Rights Reserved.
// Copyright 2021-2024 Nordcloud Oy or its affiliates. All Rights Reserved.

package imagetemplate

Expand Down Expand Up @@ -109,6 +109,18 @@ func expandVMImageDefinitionTemplateAzureConfig(in []interface{}) *graphql.NewVM
}
}

func expandAdditionalDataDisks(in []interface{}) *[]graphql.NewAdditionalDataDisks {
out := []graphql.NewAdditionalDataDisks{}
for i := range in {
m := in[i].(map[string]interface{})
out = append(out, graphql.NewAdditionalDataDisks{
Size: graphql.Int(m["size"].(int)),
})
}

return &out
}

func expandTemplateAzureConfig(in []interface{}) *graphql.NewTemplateAZUREConfig {
if len(in) == 0 {
return nil
Expand All @@ -132,6 +144,10 @@ func expandTemplateAzureConfig(in []interface{}) *graphql.NewTemplateAZUREConfig
VmImageDefinition: expandVMImageDefinitionTemplateAzureConfig(m["vm_image_definition"].([]interface{})),
}

if m["additional_data_disks"] != nil {
out.AdditionalDataDisks = expandAdditionalDataDisks(m["additional_data_disks"].([]interface{}))
}

return out
}

Expand Down
38 changes: 26 additions & 12 deletions pkg/graphql/graphql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 26 additions & 4 deletions pkg/graphql/schema.graphql
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Copyright 2021-2023 Nordcloud Oy or its affiliates. All Rights Reserved.
# Copyright 2021-2024 Nordcloud Oy or its affiliates. All Rights Reserved.

enum AccountStatus {
ACCESS_SUCCESS
Expand Down Expand Up @@ -359,6 +359,7 @@ enum OSSubtype {
REDHAT
SUSE
ORACLE_LINUX
ROCKY
}

enum OSFamily {
Expand All @@ -369,6 +370,7 @@ enum OSFamily {
REDHAT
SUSE
ORACLE_LINUX
ROCKY
}

enum SortOrder {
Expand Down Expand Up @@ -643,7 +645,7 @@ type DistributionResults {
}


# Copyright 2022-2023 Nordcloud Oy or its affiliates. All Rights Reserved.
# Copyright 2022-2024 Nordcloud Oy or its affiliates. All Rights Reserved.

type ImageComponent {
id: String!
Expand Down Expand Up @@ -684,8 +686,10 @@ type ImageCloudDistribution {
enum ImageStatus {
PENDING
CREATED
DELETED
ERROR
SHARE_ERROR
DELETE_ERROR
}

type ImageState {
Expand Down Expand Up @@ -914,7 +918,7 @@ type SettingsResult {
}


# Copyright 2021-2023 Nordcloud Oy or its affiliates. All Rights Reserved.
# Copyright 2021-2024 Nordcloud Oy or its affiliates. All Rights Reserved.

enum RebuildReasonType {
SECURITY_CHECK
Expand Down Expand Up @@ -1002,11 +1006,16 @@ type VMImageDefinition {
sku: String!
}

type AdditionalDataDisks {
size: Int!
}

type TemplateAZUREConfig {
replicaRegions: [String]
excludeFromLatest: Boolean
vmImageDefinition: VMImageDefinition
eolDateOption: Boolean
additionalDataDisks: [AdditionalDataDisks!]
}

type TemplateExoscaleConfig {
Expand Down Expand Up @@ -1139,11 +1148,24 @@ input NewVMImageDefinition {
sku: String!
}

input NewAdditionalDataDisks {
"""
size is in GB, from 1G to 10G.
"""
size: Int!
}

input NewTemplateAZUREConfig {
replicaRegions: [String]
excludeFromLatest: Boolean
vmImageDefinition: NewVMImageDefinition
eolDateOption: Boolean

"""
`additionalDataDisks` defines extra data disks attached to the image with a limit of 10.
"""
additionalDataDisks: [NewAdditionalDataDisks!]
}

input NewTemplateExoscaleConfig {
Expand Down Expand Up @@ -1195,7 +1217,7 @@ input NewTemplateConfig {
cloudAccountIds: [String]

"""
disableCyclicalRebuilds defines if cyclical rebuilds are disabled for the template
disableCyclicalRebuilds defines if cyclical rebuilds are disabled for the template.
Cyclical rebuilds are rebuilds that are triggered automatically by ImageFactory when the source image is updated or
when there are security updates available for the packages installed in the image. If cyclical rebuilds are disabled,
Expand Down

0 comments on commit a9d1f66

Please sign in to comment.