Skip to content

Commit

Permalink
Merge pull request #611 from SumoLogic/sjain-app-install-uninstall
Browse files Browse the repository at this point in the history
Add support for install, uninstall and upgrade apps in tf provider
  • Loading branch information
sjain05 authored Jan 30, 2024
2 parents dd3cadc + 639d83c commit 58474e1
Show file tree
Hide file tree
Showing 5 changed files with 454 additions and 0 deletions.
1 change: 1 addition & 0 deletions sumologic/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ func Provider() terraform.ResourceProvider {
},
},
ResourcesMap: map[string]*schema.Resource{
"sumologic_app": resourceSumologicApp(),
"sumologic_cse_tag_schema": resourceSumologicCSETagSchema(),
"sumologic_cse_context_action": resourceSumologicCSEContextAction(),
"sumologic_cse_automation": resourceSumologicCSEAutomation(),
Expand Down
138 changes: 138 additions & 0 deletions sumologic/resource_sumologic_app.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package sumologic

import (
"encoding/json"
"fmt"
"log"

"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
)

func resourceSumologicApp() *schema.Resource {
return &schema.Resource{
Create: resourceSumologicAppCreate,
Read: resourceSumologicAppRead,
Delete: resourceSumologicAppDelete,
Update: resourceSumologicAppUpdate,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},

Schema: map[string]*schema.Schema{
"uuid": {
Type: schema.TypeString,
Required: true,
},
"version": {
Type: schema.TypeString,
Required: true,
},
"parameters": {
Type: schema.TypeMap,
Optional: true,
Elem: &schema.Schema{
Type: schema.TypeString,
},
},
},
}
}

func resourceSumologicAppCreate(d *schema.ResourceData, meta interface{}) error {
c := meta.(*Client)
if d.Id() == "" {
uuid := d.Get("uuid").(string)
version := d.Get("version").(string)
parameters := d.Get("parameters").(map[string]interface{})

appInstallPayload := AppInstallPayload{
VERSION: version,
PARAMETERS: parameters,
}

log.Println("=====================================================================")
log.Printf("Installing app; uuid: %+v, version: %+v\n", uuid, version)
log.Println("=====================================================================")

appInstanceId, err := c.CreateAppInstance(uuid, appInstallPayload)
if err != nil {
return err
}
d.SetId(appInstanceId)
}

return resourceSumologicAppRead(d, meta)
}

func resourceSumologicAppRead(d *schema.ResourceData, meta interface{}) error {
c := meta.(*Client)

id := d.Id()
appInstance, err := c.GetAppInstance(id)
log.Println("=====================================================================")
log.Printf("Read app instance: %+v\n", appInstance)
log.Println("=====================================================================")
if err != nil {
return err
}

if appInstance == nil {
log.Printf("[WARN] AppInstance not found, removing from state: %v - %v", id, err)
d.SetId("")
return nil
}

var parameters map[string]interface{}
if err := json.Unmarshal([]byte(appInstance.CONFIGURATIONBLOB), &parameters); err != nil {
return err
}
d.Set("uuid", appInstance.UUID)
d.Set("version", appInstance.VERSION)
d.Set("parameters", parameters)
d.SetId(appInstance.ID)

return nil
}

func resourceSumologicAppDelete(d *schema.ResourceData, meta interface{}) error {
c := meta.(*Client)
uuid := d.Get("uuid").(string)
log.Printf("Uninstalling app: %+v\n", uuid)
return c.DeleteAppInstance(uuid)
}

func resourceSumologicAppUpdate(d *schema.ResourceData, meta interface{}) error {
c := meta.(*Client)

uuid := d.Get("uuid").(string)

// ensure that uuid matches with already installed instance's uuid
appInstance, err := c.GetAppInstance(d.Id())
if err != nil {
return err
}
if uuid == appInstance.UUID {
version := d.Get("version").(string)
if version == appInstance.VERSION {
return nil
}
parameters := d.Get("parameters").(map[string]interface{})
appInstallPayload := AppInstallPayload{
VERSION: version,
PARAMETERS: parameters,
}

log.Println("=====================================================================")
log.Printf("Upgrading app; uuid: %+v, version: %+v\n", uuid, version)
log.Println("=====================================================================")

_, err := c.UpdateAppInstance(uuid, appInstallPayload)

if err != nil {
return err
}
return resourceSumologicAppRead(d, meta)
}

return fmt.Errorf("uuid is incorrect")
}
156 changes: 156 additions & 0 deletions sumologic/resource_sumologic_app_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,156 @@
package sumologic

import (
"fmt"
"strconv"
"strings"
"testing"

"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/terraform"
)

func TestAccSumologicApp_basic(t *testing.T) {
// uuid of Varnish - OpenTelemetry app
uuid := "d2ef33c3-67f2-4438-9124-14a30ec2ecf3"
version := "1.0.3"
parameterKey := "key"
parameterValue := "value"

tfResourceName := "tf_app_test"
resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAppDestroy(),
Steps: []resource.TestStep{
{
Config: testAccSumologicApp(tfResourceName, uuid, version, parameterKey, parameterValue),
},
{
ResourceName: fmt.Sprintf("sumologic_app.%s", tfResourceName),
ImportState: true,
ImportStateVerify: true,
},
},
})
}

func TestAccSumologicApp_create(t *testing.T) {
// create config
// uuid of Varnish - OpenTelemetry app
uuid := "d2ef33c3-67f2-4438-9124-14a30ec2ecf3"
version := "1.0.4"
parameterKey := "key"
parameterValue := "value"

tfResourceName := "tf_app_test"
tfAppResource := fmt.Sprintf("sumologic_app.%s", tfResourceName)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAppDestroy(),
Steps: []resource.TestStep{
{
Config: testAccSumologicApp(tfResourceName, uuid, version, parameterKey, parameterValue),
Check: resource.ComposeTestCheckFunc(
testAccCheckAppInstanceExists(tfAppResource, t),
resource.TestCheckResourceAttr(tfAppResource, "uuid", uuid),
resource.TestCheckResourceAttr(tfAppResource, "version", version),
resource.TestCheckResourceAttr(tfAppResource, "parameters.%", "1"),
resource.TestCheckResourceAttr(tfAppResource, "parameters.key", "value"),
),
},
},
})
}

func TestAccSumologicApp_update(t *testing.T) {

// create config
// uuid of Varnish - OpenTelemetry app
uuid := "d2ef33c3-67f2-4438-9124-14a30ec2ecf3"
version := "1.0.3"
parameterKey := "key"
parameterValue := "value"

tfResourceName := "tf_app_test"
tfAppResource := fmt.Sprintf("sumologic_app.%s", tfResourceName)

resource.Test(t, resource.TestCase{
PreCheck: func() { testAccPreCheck(t) },
Providers: testAccProviders,
CheckDestroy: testAccCheckAppDestroy(),
Steps: []resource.TestStep{
{
Config: testAccSumologicApp(tfResourceName, uuid, version, parameterKey, parameterValue),
Check: resource.ComposeTestCheckFunc(
testAccCheckAppInstanceExists(tfAppResource, t),
resource.TestCheckResourceAttr(tfAppResource, "uuid", uuid),
resource.TestCheckResourceAttr(tfAppResource, "version", version),
resource.TestCheckResourceAttr(tfAppResource, "parameters.%", "1"),
resource.TestCheckResourceAttr(tfAppResource, "parameters.key", "value"),
),
},
{
Config: testAccSumologicApp(tfResourceName, uuid, "1.0.4", parameterKey, parameterValue),
Check: resource.ComposeTestCheckFunc(
testAccCheckAppInstanceExists(tfAppResource, t),
resource.TestCheckResourceAttr(tfAppResource, "uuid", uuid),
resource.TestCheckResourceAttr(tfAppResource, "version", "1.0.4"),
resource.TestCheckResourceAttr(tfAppResource, "parameters.%", "1"),
resource.TestCheckResourceAttr(tfAppResource, "parameters.key", "value"),
),
},
},
})
}

func testAccCheckAppDestroy() resource.TestCheckFunc {
return func(s *terraform.State) error {
client := testAccProvider.Meta().(*Client)
for _, r := range s.RootModule().Resources {
id := r.Primary.ID
appInstance, _ := client.GetAppInstance(id)
if appInstance != nil {
return fmt.Errorf("AppInstance %s still exists", id)
}
}
return nil
}
}

func testAccCheckAppInstanceExists(name string, t *testing.T) resource.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[name]
if !ok {
return fmt.Errorf("Error = %s. App Instance not found: %s", strconv.FormatBool(ok), name)
}

if strings.EqualFold(rs.Primary.ID, "") {
return fmt.Errorf("App Instance ID is not set")
}

id := rs.Primary.ID
client := testAccProvider.Meta().(*Client)
_, err := client.GetAppInstance(id)
if err != nil {
return fmt.Errorf("App instance (id=%s) not found", id)
}
return nil
}
}

func testAccSumologicApp(tfResourceName string, uuid string, version string, parameterKey string, parameterValue string) string {

return fmt.Sprintf(`
resource "sumologic_app" "%s" {
uuid = "%s"
version = "%s"
parameters = {
"%s": "%s"
}
}
`, tfResourceName, uuid, version, parameterKey, parameterValue)
}
Loading

0 comments on commit 58474e1

Please sign in to comment.