Skip to content

Commit d2602c2

Browse files
PHR-16318 - add QuestionnaireTheme terraform provider to allow adding of default wayfinder theme (#159)
* PHR-16318 - add QuestionnaireTheme terraform provider to allow adding of default wayfinder theme
1 parent 6ba2e34 commit d2602c2

File tree

6 files changed

+273
-0
lines changed

6 files changed

+273
-0
lines changed

aidbox/questionnaire_theme.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package aidbox
2+
3+
import (
4+
"context"
5+
)
6+
7+
// QuestionnaireTheme Represents the custom aidbox resource spec "QuestionnaireTheme"
8+
// https://www.health-samurai.io/docs/aidbox/reference/system-resources-reference/sdc-module-resources#questionnairetheme
9+
type QuestionnaireTheme struct {
10+
ResourceBase
11+
ThemeName string `json:"theme-name,omitempty"`
12+
DesignSystem string `json:"design-system,omitempty"`
13+
}
14+
15+
func (*QuestionnaireTheme) GetResourcePath() string {
16+
return "QuestionnaireTheme"
17+
}
18+
19+
func (apiClient *ApiClient) CreateQuestionnaireTheme(ctx context.Context, theme *QuestionnaireTheme) (*QuestionnaireTheme, error) {
20+
response := &QuestionnaireTheme{}
21+
return response, apiClient.updateResource(ctx, theme, response)
22+
}
23+
24+
func (apiClient *ApiClient) GetQuestionnaireTheme(ctx context.Context, id string) (*QuestionnaireTheme, error) {
25+
response := &QuestionnaireTheme{}
26+
return response, apiClient.getResource(ctx, id, response)
27+
}
28+
29+
func (apiClient *ApiClient) UpdateQuestionnaireTheme(ctx context.Context, theme *QuestionnaireTheme) (*QuestionnaireTheme, error) {
30+
response := &QuestionnaireTheme{}
31+
return response, apiClient.updateResource(ctx, theme, response)
32+
}
33+
34+
func (apiClient *ApiClient) DeleteQuestionnaireTheme(ctx context.Context, id string) error {
35+
return apiClient.deleteResource(ctx, id, &QuestionnaireTheme{})
36+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "aidbox_questionnaire_theme Resource - terraform-provider-aidbox"
4+
subcategory: ""
5+
description: |-
6+
QuestionnaireTheme https://www.health-samurai.io/docs/aidbox/reference/system-resources-reference/sdc-module-resources#questionnairetheme
7+
---
8+
9+
# aidbox_questionnaire_theme (Resource)
10+
11+
QuestionnaireTheme https://www.health-samurai.io/docs/aidbox/reference/system-resources-reference/sdc-module-resources#questionnairetheme
12+
13+
## Example Usage
14+
15+
```terraform
16+
resource "aidbox_questionnaire_theme" "nhs_wayfinder_theme" {
17+
aidbox_id = "nhs-wayfinder-theme"
18+
theme_name = "Wayfinder Theme"
19+
design_system = "NHS"
20+
}
21+
```
22+
23+
<!-- schema generated by tfplugindocs -->
24+
## Schema
25+
26+
### Required
27+
28+
- `aidbox_id` (String) The Aidbox ID of the questionnaire theme
29+
30+
### Optional
31+
32+
- `design_system` (String) Design system of the theme
33+
- `theme_name` (String) Name of the theme
34+
35+
### Read-Only
36+
37+
- `id` (String) The ID of this resource.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
resource "aidbox_questionnaire_theme" "nhs_wayfinder_theme" {
2+
aidbox_id = "nhs-wayfinder-theme"
3+
theme_name = "Wayfinder Theme"
4+
design_system = "NHS"
5+
}

internal/provider/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,7 @@ func New(apiClient *aidbox.ApiClient) func() *schema.Provider {
6666
"aidbox_structure_definition_override": resourceStructureDefinitionOverride(),
6767
"aidbox_sdc_config": resourceSDCConfig(),
6868
"aidbox_gcp_service_account": resourceGcpServiceAccount(),
69+
"aidbox_questionnaire_theme": resourceQuestionnaireTheme(),
6970
},
7071
}
7172

Lines changed: 134 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,134 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
6+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
7+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
8+
"github.com/patientsknowbest/terraform-provider-aidbox/aidbox"
9+
)
10+
11+
func resourceQuestionnaireTheme() *schema.Resource {
12+
return &schema.Resource{
13+
Description: "QuestionnaireTheme https://www.health-samurai.io/docs/aidbox/reference/system-resources-reference/sdc-module-resources#questionnairetheme",
14+
CreateContext: resourceQuestionnaireThemeCreate,
15+
ReadContext: resourceQuestionnaireThemeRead,
16+
UpdateContext: resourceQuestionnaireThemeUpdate,
17+
DeleteContext: resourceQuestionnaireThemeDelete,
18+
Importer: &schema.ResourceImporter{
19+
StateContext: resourceQuestionnaireThemeImport,
20+
},
21+
Schema: resourceFullSchema(resourceSchemaQuestionnaireTheme()),
22+
}
23+
}
24+
25+
func resourceSchemaQuestionnaireTheme() map[string]*schema.Schema {
26+
return map[string]*schema.Schema{
27+
"aidbox_id": {
28+
Description: "The Aidbox ID of the questionnaire theme",
29+
Type: schema.TypeString,
30+
Required: true,
31+
ForceNew: true,
32+
},
33+
"theme_name": {
34+
Description: "Name of the theme",
35+
Type: schema.TypeString,
36+
Optional: true,
37+
},
38+
"design_system": {
39+
Description: "Design system of the theme",
40+
Type: schema.TypeString,
41+
Optional: true,
42+
},
43+
}
44+
}
45+
46+
func mapQuestionnaireThemeToData(res *aidbox.QuestionnaireTheme, data *schema.ResourceData) error {
47+
data.SetId(res.ID)
48+
if err := data.Set("aidbox_id", res.ID); err != nil {
49+
return err
50+
}
51+
if err := data.Set("theme_name", res.ThemeName); err != nil {
52+
return err
53+
}
54+
if err := data.Set("design_system", res.DesignSystem); err != nil {
55+
return err
56+
}
57+
return nil
58+
}
59+
60+
func mapQuestionnaireThemeFromData(d *schema.ResourceData) *aidbox.QuestionnaireTheme {
61+
res := &aidbox.QuestionnaireTheme{}
62+
if v, ok := d.GetOk("aidbox_id"); ok {
63+
res.ID = v.(string)
64+
}
65+
if v, ok := d.GetOk("theme_name"); ok {
66+
res.ThemeName = v.(string)
67+
}
68+
if v, ok := d.GetOk("design_system"); ok {
69+
res.DesignSystem = v.(string)
70+
}
71+
return res
72+
}
73+
74+
func resourceQuestionnaireThemeCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
75+
apiClient := meta.(*aidbox.ApiClient)
76+
q := mapQuestionnaireThemeFromData(d)
77+
res, err := apiClient.CreateQuestionnaireTheme(ctx, q)
78+
if err != nil {
79+
return diag.FromErr(err)
80+
}
81+
if err := mapQuestionnaireThemeToData(res, d); err != nil {
82+
return diag.FromErr(err)
83+
}
84+
return nil
85+
}
86+
87+
func resourceQuestionnaireThemeRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
88+
apiClient := meta.(*aidbox.ApiClient)
89+
res, err := apiClient.GetQuestionnaireTheme(ctx, d.Id())
90+
if err != nil {
91+
if handleNotFoundError(err, d) {
92+
return nil
93+
}
94+
return diag.FromErr(err)
95+
}
96+
if err := mapQuestionnaireThemeToData(res, d); err != nil {
97+
return diag.FromErr(err)
98+
}
99+
return nil
100+
}
101+
102+
func resourceQuestionnaireThemeUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
103+
apiClient := meta.(*aidbox.ApiClient)
104+
q := mapQuestionnaireThemeFromData(d)
105+
res, err := apiClient.UpdateQuestionnaireTheme(ctx, q)
106+
if err != nil {
107+
return diag.FromErr(err)
108+
}
109+
if err := mapQuestionnaireThemeToData(res, d); err != nil {
110+
return diag.FromErr(err)
111+
}
112+
return nil
113+
}
114+
115+
func resourceQuestionnaireThemeDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
116+
apiClient := meta.(*aidbox.ApiClient)
117+
err := apiClient.DeleteQuestionnaireTheme(ctx, d.Id())
118+
if err != nil {
119+
return diag.FromErr(err)
120+
}
121+
return nil
122+
}
123+
124+
func resourceQuestionnaireThemeImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
125+
apiClient := meta.(*aidbox.ApiClient)
126+
res, err := apiClient.GetQuestionnaireTheme(ctx, d.Id())
127+
if err != nil {
128+
return nil, err
129+
}
130+
if err := mapQuestionnaireThemeToData(res, d); err != nil {
131+
return nil, err
132+
}
133+
return []*schema.ResourceData{d}, nil
134+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package provider
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/resource"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/terraform"
10+
"github.com/patientsknowbest/terraform-provider-aidbox/aidbox"
11+
)
12+
13+
func TestAccQuestionnaireThemeResource(t *testing.T) {
14+
resource.Test(t, resource.TestCase{
15+
PreCheck: func() { testAccPreCheck(t) },
16+
ProviderFactories: testProviderFactories,
17+
CheckDestroy: testAccQuestionnaireThemeResourceCheckDestroy,
18+
Steps: []resource.TestStep{
19+
{
20+
Config: testAccQuestionnaireThemeResourceConfig("nhs-wayfinder-theme", "Wayfinder Theme", "NHS"),
21+
Check: resource.ComposeTestCheckFunc(
22+
resource.TestCheckResourceAttr("aidbox_questionnaire_theme.nhs_wayfinder_theme", "aidbox_id", "nhs-wayfinder-theme"),
23+
resource.TestCheckResourceAttr("aidbox_questionnaire_theme.nhs_wayfinder_theme", "theme_name", "Wayfinder Theme"),
24+
resource.TestCheckResourceAttr("aidbox_questionnaire_theme.nhs_wayfinder_theme", "design_system", "NHS"),
25+
),
26+
},
27+
{
28+
ResourceName: "aidbox_questionnaire_theme.nhs_wayfinder_theme",
29+
ImportState: true,
30+
ImportStateVerify: true,
31+
},
32+
},
33+
})
34+
}
35+
36+
func testAccQuestionnaireThemeResourceConfig(id, themeName, designSystem string) string {
37+
return fmt.Sprintf(`
38+
resource "aidbox_questionnaire_theme" "nhs_wayfinder_theme" {
39+
aidbox_id = "%s"
40+
theme_name = "%s"
41+
design_system = "%s"
42+
}
43+
`, id, themeName, designSystem)
44+
}
45+
46+
func testAccQuestionnaireThemeResourceCheckDestroy(s *terraform.State) error {
47+
apiClient := testProvider.Meta().(*aidbox.ApiClient)
48+
49+
for _, rs := range s.RootModule().Resources {
50+
if rs.Type != "aidbox_questionnaire_theme" {
51+
continue
52+
}
53+
54+
_, err := apiClient.GetQuestionnaireTheme(context.Background(), rs.Primary.ID)
55+
if err == nil {
56+
return fmt.Errorf("QuestionnaireTheme still exists")
57+
}
58+
}
59+
return nil
60+
}

0 commit comments

Comments
 (0)