Skip to content

Commit 8000037

Browse files
committed
SUMO-216512: Add test for resource
1 parent 2742250 commit 8000037

File tree

4 files changed

+339
-40
lines changed

4 files changed

+339
-40
lines changed

sumologic/provider.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ func Provider() terraform.ResourceProvider {
4343
},
4444
},
4545
ResourcesMap: map[string]*schema.Resource{
46+
"sumologic_app": resourceSumologicApp(),
4647
"sumologic_cse_tag_schema": resourceSumologicCSETagSchema(),
4748
"sumologic_cse_context_action": resourceSumologicCSEContextAction(),
4849
"sumologic_cse_automation": resourceSumologicCSEAutomation(),
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
package sumologic
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"log"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
9+
)
10+
11+
func resourceSumologicApp() *schema.Resource {
12+
return &schema.Resource{
13+
Create: resourceSumologicAppCreate,
14+
Read: resourceSumologicAppRead,
15+
Delete: resourceSumologicAppDelete,
16+
Update: resourceSumologicAppUpdate,
17+
Importer: &schema.ResourceImporter{
18+
State: schema.ImportStatePassthrough,
19+
},
20+
21+
Schema: map[string]*schema.Schema{
22+
"uuid": {
23+
Type: schema.TypeString,
24+
Required: true,
25+
},
26+
"version": {
27+
Type: schema.TypeString,
28+
Required: true,
29+
},
30+
"parameters": {
31+
Type: schema.TypeMap,
32+
Optional: true,
33+
Elem: &schema.Schema{
34+
Type: schema.TypeString,
35+
},
36+
},
37+
},
38+
}
39+
}
40+
41+
func resourceSumologicAppCreate(d *schema.ResourceData, meta interface{}) error {
42+
c := meta.(*Client)
43+
if d.Id() == "" {
44+
uuid := d.Get("uuid").(string)
45+
version := d.Get("version").(string)
46+
parameters := d.Get("parameters").(map[string]interface{})
47+
48+
appInstallPayload := AppInstallPayload{
49+
VERSION: version,
50+
PARAMETERS: parameters,
51+
}
52+
53+
log.Println("=====================================================================")
54+
log.Printf("Installing app; uuid: %+v, version: %+v\n", uuid, version)
55+
log.Println("=====================================================================")
56+
57+
appInstanceId, err := c.CreateAppInstance(uuid, appInstallPayload)
58+
if err != nil {
59+
return err
60+
}
61+
d.SetId(appInstanceId)
62+
}
63+
64+
return resourceSumologicAppRead(d, meta)
65+
}
66+
67+
func resourceSumologicAppRead(d *schema.ResourceData, meta interface{}) error {
68+
c := meta.(*Client)
69+
70+
id := d.Id()
71+
appInstance, err := c.GetAppInstance(id)
72+
log.Println("=====================================================================")
73+
log.Printf("Read app instance: %+v\n", appInstance)
74+
log.Println("=====================================================================")
75+
if err != nil {
76+
return err
77+
}
78+
79+
if appInstance == nil {
80+
log.Printf("[WARN] AppInstance not found, removing from state: %v - %v", id, err)
81+
d.SetId("")
82+
return nil
83+
}
84+
85+
var parameters map[string]interface{}
86+
if err := json.Unmarshal([]byte(appInstance.CONFIGURATIONBLOB), &parameters); err != nil {
87+
return err
88+
}
89+
d.Set("uuid", appInstance.UUID)
90+
d.Set("version", appInstance.VERSION)
91+
d.Set("parameters", parameters)
92+
d.SetId(appInstance.ID)
93+
94+
return nil
95+
}
96+
97+
func resourceSumologicAppDelete(d *schema.ResourceData, meta interface{}) error {
98+
c := meta.(*Client)
99+
uuid := d.Get("uuid").(string)
100+
log.Printf("Uninstalling app: %+v\n", uuid)
101+
return c.DeleteAppInstance(uuid)
102+
}
103+
104+
func resourceSumologicAppUpdate(d *schema.ResourceData, meta interface{}) error {
105+
c := meta.(*Client)
106+
107+
uuid := d.Get("uuid").(string)
108+
109+
// ensure that uuid matches with already installed instance's uuid
110+
appInstance, err := c.GetAppInstance(d.Id())
111+
if err != nil {
112+
return err
113+
}
114+
if uuid == appInstance.UUID {
115+
version := d.Get("version").(string)
116+
if version == appInstance.VERSION {
117+
return nil
118+
}
119+
parameters := d.Get("parameters").(map[string]interface{})
120+
appInstallPayload := AppInstallPayload{
121+
VERSION: version,
122+
PARAMETERS: parameters,
123+
}
124+
125+
log.Println("=====================================================================")
126+
log.Printf("Upgrading app; uuid: %+v, version: %+v\n", uuid, version)
127+
log.Println("=====================================================================")
128+
129+
_, err := c.UpdateAppInstance(uuid, appInstallPayload)
130+
131+
if err != nil {
132+
return err
133+
}
134+
return resourceSumologicAppRead(d, meta)
135+
}
136+
137+
return fmt.Errorf("uuid is incorrect")
138+
}
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
package sumologic
2+
3+
import (
4+
"fmt"
5+
"strconv"
6+
"strings"
7+
"testing"
8+
9+
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
10+
"github.com/hashicorp/terraform-plugin-sdk/terraform"
11+
)
12+
13+
func TestAccSumologicApp_basic(t *testing.T) {
14+
// uuid of MySql - OpenTelemetry app
15+
uuid := "1de46d03-e051-404e-8c6b-53aa7a057df6"
16+
version := "1.0.5"
17+
parameterKey := "key"
18+
parameterValue := "value"
19+
20+
tfResourceName := "tf_app_test"
21+
resource.Test(t, resource.TestCase{
22+
PreCheck: func() { testAccPreCheck(t) },
23+
Providers: testAccProviders,
24+
CheckDestroy: testAccCheckAppDestroy(),
25+
Steps: []resource.TestStep{
26+
{
27+
Config: testAccSumologicApp(tfResourceName, uuid, version, parameterKey, parameterValue),
28+
},
29+
{
30+
ResourceName: fmt.Sprintf("sumologic_app.%s", tfResourceName),
31+
ImportState: true,
32+
ImportStateVerify: true,
33+
},
34+
},
35+
})
36+
}
37+
38+
func TestAccSumologicApp_create(t *testing.T) {
39+
// create config
40+
// uuid of MySql - OpenTelemetry app
41+
uuid := "1de46d03-e051-404e-8c6b-53aa7a057df6"
42+
version := "1.0.5"
43+
parameterKey := "key"
44+
parameterValue := "value"
45+
46+
tfResourceName := "tf_app_test"
47+
tfAppResource := fmt.Sprintf("sumologic_app.%s", tfResourceName)
48+
49+
resource.Test(t, resource.TestCase{
50+
PreCheck: func() { testAccPreCheck(t) },
51+
Providers: testAccProviders,
52+
CheckDestroy: testAccCheckAppDestroy(),
53+
Steps: []resource.TestStep{
54+
{
55+
Config: testAccSumologicApp(tfResourceName, uuid, version, parameterKey, parameterValue),
56+
Check: resource.ComposeTestCheckFunc(
57+
testAccCheckAppInstanceExists(tfAppResource, t),
58+
resource.TestCheckResourceAttr(tfAppResource, "uuid", uuid),
59+
resource.TestCheckResourceAttr(tfAppResource, "version", version),
60+
resource.TestCheckResourceAttr(tfAppResource, "parameters.%", "1"),
61+
resource.TestCheckResourceAttr(tfAppResource, "parameters.key", "value"),
62+
),
63+
},
64+
},
65+
})
66+
}
67+
68+
func TestAccSumologicApp_update(t *testing.T) {
69+
70+
// create config
71+
// uuid of Varnish - OpenTelemetry app
72+
uuid := "d2ef33c3-67f2-4438-9124-14a30ec2ecf3"
73+
version := "1.0.3"
74+
parameterKey := "key"
75+
parameterValue := "value"
76+
77+
tfResourceName := "tf_app_test"
78+
tfAppResource := fmt.Sprintf("sumologic_app.%s", tfResourceName)
79+
80+
resource.Test(t, resource.TestCase{
81+
PreCheck: func() { testAccPreCheck(t) },
82+
Providers: testAccProviders,
83+
CheckDestroy: testAccCheckAppDestroy(),
84+
Steps: []resource.TestStep{
85+
{
86+
Config: testAccSumologicApp(tfResourceName, uuid, version, parameterKey, parameterValue),
87+
Check: resource.ComposeTestCheckFunc(
88+
testAccCheckAppInstanceExists(tfAppResource, t),
89+
resource.TestCheckResourceAttr(tfAppResource, "uuid", uuid),
90+
resource.TestCheckResourceAttr(tfAppResource, "version", version),
91+
resource.TestCheckResourceAttr(tfAppResource, "parameters.%", "1"),
92+
resource.TestCheckResourceAttr(tfAppResource, "parameters.key", "value"),
93+
),
94+
},
95+
{
96+
Config: testAccSumologicApp(tfResourceName, uuid, "1.0.4", parameterKey, parameterValue),
97+
Check: resource.ComposeTestCheckFunc(
98+
testAccCheckAppInstanceExists(tfAppResource, t),
99+
resource.TestCheckResourceAttr(tfAppResource, "uuid", uuid),
100+
resource.TestCheckResourceAttr(tfAppResource, "version", "1.0.4"),
101+
resource.TestCheckResourceAttr(tfAppResource, "parameters.%", "1"),
102+
resource.TestCheckResourceAttr(tfAppResource, "parameters.key", "value"),
103+
),
104+
},
105+
},
106+
})
107+
}
108+
109+
func testAccCheckAppDestroy() resource.TestCheckFunc {
110+
return func(s *terraform.State) error {
111+
client := testAccProvider.Meta().(*Client)
112+
for _, r := range s.RootModule().Resources {
113+
id := r.Primary.ID
114+
appInstance, _ := client.GetAppInstance(id)
115+
if appInstance != nil {
116+
return fmt.Errorf("AppInstance %s still exists", id)
117+
}
118+
}
119+
return nil
120+
}
121+
}
122+
123+
func testAccCheckAppInstanceExists(name string, t *testing.T) resource.TestCheckFunc {
124+
return func(s *terraform.State) error {
125+
rs, ok := s.RootModule().Resources[name]
126+
if !ok {
127+
return fmt.Errorf("Error = %s. App Instance not found: %s", strconv.FormatBool(ok), name)
128+
}
129+
130+
if strings.EqualFold(rs.Primary.ID, "") {
131+
return fmt.Errorf("App Instance ID is not set")
132+
}
133+
134+
id := rs.Primary.ID
135+
client := testAccProvider.Meta().(*Client)
136+
_, err := client.GetAppInstance(id)
137+
if err != nil {
138+
return fmt.Errorf("App instance (id=%s) not found", id)
139+
}
140+
return nil
141+
}
142+
}
143+
144+
func testAccSumologicApp(tfResourceName string, uuid string, version string, parameterKey string, parameterValue string) string {
145+
146+
return fmt.Sprintf(`
147+
148+
resource "sumologic_app" "%s" {
149+
uuid = "%s"
150+
version = "%s"
151+
parameters = {
152+
"%s": "%s"
153+
}
154+
}
155+
`, tfResourceName, uuid, version, parameterKey, parameterValue)
156+
}

0 commit comments

Comments
 (0)