-
Notifications
You must be signed in to change notification settings - Fork 57
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #611 from SumoLogic/sjain-app-install-uninstall
Add support for install, uninstall and upgrade apps in tf provider
- Loading branch information
Showing
5 changed files
with
454 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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), ¶meters); 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") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
Oops, something went wrong.