Skip to content

Commit

Permalink
feat: rebuild templates when a component changes (#62)
Browse files Browse the repository at this point in the history
* Add to rebuild templates when a component changes

* Refactor from linter

* Update license headers

---------

Co-authored-by: sternik <[email protected]>
  • Loading branch information
sternik and sternik authored Feb 29, 2024
1 parent e105118 commit 8f0a520
Show file tree
Hide file tree
Showing 11 changed files with 218 additions and 61 deletions.
9 changes: 4 additions & 5 deletions .golangci.yml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
# Copyright 2021 Nordcloud Oy or its affiliates. All Rights Reserved.
# Copyright 2021-2024 Nordcloud Oy or its affiliates. All Rights Reserved.

run:
concurrency: 4
timeout: 6m
issues-exit-code: 0
# Put here project specific
issues-exit-code: 0
# Put here project specific
skip-dirs:
- ui
- api
- api
- gql
- build
- artifacts
Expand Down Expand Up @@ -73,7 +73,6 @@ linters:
enable:
- bodyclose
- deadcode
- depguard
- dogsled
- dupl
- errcheck
Expand Down
4 changes: 2 additions & 2 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ Please check https://klarity.nordcloud.com to learn more about the ImageFactory

```terraform
/**
* Copyright 2021-2023 Nordcloud Oy or its affiliates. All Rights Reserved.
* Copyright 2021-2024 Nordcloud Oy or its affiliates. All Rights Reserved.
*/
terraform {
required_version = ">= 0.14"
required_providers {
imagefactory = {
source = "nordcloud/imagefactory"
version = "1.8.4"
version = "1.8.5"
}
}
}
Expand Down
29 changes: 8 additions & 21 deletions docs/resources/custom_component.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,32 +38,18 @@ output "shell_component" {
# An example of a Powershell component
resource "imagefactory_custom_component" "powershell_component" {
name = "Install Apache"
description = "Install Apache HTTP Server on Microsoft Windows"
name = "Install nginx"
description = "Install nginx Server on Microsoft Windows"
stage = "BUILD"
cloud_providers = ["AWS", "AZURE"]
os_types = ["WINDOWS"]
content {
script = <<-EOT
---
- name: Installing Apache HTTP Server
hosts: all
tasks:
- name: Create directory structure
ansible.windows.win_file:
path: C:\ansible_examples
state: directory
- name: Download the Apache installer
win_get_url:
url: https://archive.apache.org/dist/httpd/binaries/win32/httpd-2.2.25-win32-x86-no_ssl.msi
dest: C:\ansible_examples\httpd-2.2.25-win32-x86-no_ssl.msi
- name: Install MSI
win_package:
path: C:\ansible_examples\httpd-2.2.25-win32-x86-no_ssl.msi
state: present
$ErrorActionPreference = 'Stop';
$ProgressPreference = 'SilentlyContinue';
Invoke-WebRequest -Method Get -Uri https://nginx.org/download/nginx-1.25.4.zip -OutFile c:\nginx-1.25.4.zip ;
Expand-Archive -Path c:\nginx-1.25.4.zip -DestinationPath c:\ ;
Remove-Item c:\nginx-1.25.4.zip -Force
EOT
provisioner = "POWERSHELL"
}
Expand Down Expand Up @@ -123,6 +109,7 @@ output "ansible_component" {
### Optional

- `description` (String)
- `rebuild_templates` (Boolean) Trigger rebuild of templates using this component. Only applicable when content is updated. Default is false.

### Read-Only

Expand Down
2 changes: 1 addition & 1 deletion examples/provider/provider.tf
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ terraform {
required_providers {
imagefactory = {
source = "nordcloud/imagefactory"
version = "1.8.4"
version = "1.8.5"
}
}
}
Expand Down
30 changes: 8 additions & 22 deletions examples/resources/imagefactory_custom_component/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.

# An example of a SHELL component

Expand All @@ -23,32 +23,18 @@ output "shell_component" {
# An example of a Powershell component

resource "imagefactory_custom_component" "powershell_component" {
name = "Install Apache"
description = "Install Apache HTTP Server on Microsoft Windows"
name = "Install nginx"
description = "Install nginx Server on Microsoft Windows"
stage = "BUILD"
cloud_providers = ["AWS", "AZURE"]
os_types = ["WINDOWS"]
content {
script = <<-EOT
---
- name: Installing Apache HTTP Server
hosts: all
tasks:
- name: Create directory structure
ansible.windows.win_file:
path: C:\ansible_examples
state: directory
- name: Download the Apache installer
win_get_url:
url: https://archive.apache.org/dist/httpd/binaries/win32/httpd-2.2.25-win32-x86-no_ssl.msi
dest: C:\ansible_examples\httpd-2.2.25-win32-x86-no_ssl.msi
- name: Install MSI
win_package:
path: C:\ansible_examples\httpd-2.2.25-win32-x86-no_ssl.msi
state: present
$ErrorActionPreference = 'Stop';
$ProgressPreference = 'SilentlyContinue';
Invoke-WebRequest -Method Get -Uri https://nginx.org/download/nginx-1.25.4.zip -OutFile c:\nginx-1.25.4.zip ;
Expand-Archive -Path c:\nginx-1.25.4.zip -DestinationPath c:\ ;
Remove-Item c:\nginx-1.25.4.zip -Force
EOT
provisioner = "POWERSHELL"
}
Expand Down
12 changes: 9 additions & 3 deletions imagefactory/component/resource.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 component

Expand Down Expand Up @@ -37,7 +37,7 @@ func resourceComponentCreate(ctx context.Context, d *schema.ResourceData, m inte
Providers: expandProviders(d.Get("cloud_providers").([]interface{})),
Content: expandContent(d.Get("content").([]interface{})),
}
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 @@ -79,7 +79,7 @@ func resourceComponentUpdate(ctx context.Context, d *schema.ResourceData, m inte
OsTypes: expandOSTypes(d.Get("os_types").([]interface{})),
Providers: expandProviders(d.Get("cloud_providers").([]interface{})),
}
if len(d.Get("description").(string)) > 0 {
if d.Get("description").(string) != "" {
description := graphql.String(d.Get("description").(string))
input.Description = &description
}
Expand All @@ -104,6 +104,12 @@ func resourceComponentUpdate(ctx context.Context, d *schema.ResourceData, m inte
if err != nil {
return diag.FromErr(err)
}

if d.Get("rebuild_templates").(bool) {
if err := c.APIClient.RebuildTemplatesUsingComponent(componentID); err != nil {
return diag.FromErr(err)
}
}
}

return setProps(d, component)
Expand Down
8 changes: 7 additions & 1 deletion imagefactory/component/schema.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright 2021 Nordcloud Oy or its affiliates. All Rights Reserved.
// Copyright 2021-2024 Nordcloud Oy or its affiliates. All Rights Reserved.

package component

Expand Down Expand Up @@ -88,4 +88,10 @@ var componentSchema = map[string]*schema.Schema{
Required: true,
Elem: contentComponentResource,
},
"rebuild_templates": {
Type: schema.TypeBool,
Optional: true,
Description: "Trigger rebuild of templates using this component. " +
"Only applicable when content is updated. Default is false.",
},
}
86 changes: 86 additions & 0 deletions pkg/graphql/graphql.go

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

18 changes: 17 additions & 1 deletion pkg/graphql/template.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.

query GetTemplate($input: CustomerTemplateIdInput!) {
template(input: $input) {
Expand Down Expand Up @@ -30,6 +30,16 @@ query GetTemplates($input: CustomerTemplatesInput!) {
name
}
}
config {
buildComponents {
id
version
}
testComponents {
id
version
}
}
}
}
}
Expand Down Expand Up @@ -63,3 +73,9 @@ mutation UpdateTemplate($input: TemplateChanges!) {
mutation DeleteTemplate($input: CustomerTemplateIdInput!) {
deleteTemplate(input: $input)
}

mutation RebuildTemplate($input: CustomerTemplateIdInput!) {
rebuildTemplate(input: $input) {
id
}
}
5 changes: 4 additions & 1 deletion pkg/sdk/api.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 sdk

Expand Down Expand Up @@ -57,4 +57,7 @@ type API interface {
CreateVariable(input NewVariable) (Variable, error)
UpdateVariable(input NewVariable) (Variable, error)
DeleteVariable(name string) error

// Actions
RebuildTemplatesUsingComponent(componentID string) error
}
Loading

0 comments on commit 8f0a520

Please sign in to comment.