Skip to content

Commit

Permalink
Merge pull request cheelim1#19 from cheelim1/fix/issue-8
Browse files Browse the repository at this point in the history
FIX: Create App issue Crashing
  • Loading branch information
cheelim1 authored Oct 14, 2024
2 parents 22df163 + 015657d commit a735daf
Show file tree
Hide file tree
Showing 2 changed files with 110 additions and 87 deletions.
161 changes: 78 additions & 83 deletions jumpcloud/resource_application.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,20 @@ import (
jcapiv1 "github.com/TheJumpCloud/jcapi-go/v1"
jcapiv2 "github.com/TheJumpCloud/jcapi-go/v2"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"

// "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"golang.org/x/net/context"
)

type Constant struct {
name string
value string
name string
value string
read_only bool
required bool
visible bool
required bool
visible bool
}

func resourceApplication() *schema.Resource {
return &schema.Resource{
Description: "Provides a resource for adding an Amazon Web Services (AWS) account application. **Note:** This resource is due to change in future versions to be more generic and allow for adding various applications supported by JumpCloud.",
Description: "Provides a resource for adding an Amazon Web Services (AWS) account application. **Note:** This resource is due to change in future versions to be more generic and allow for adding various applications supported by JumpCloud.",
Create: resourceApplicationCreate,
Read: resourceApplicationRead,
Update: resourceApplicationUpdate,
Expand All @@ -38,8 +36,8 @@ func resourceApplication() *schema.Resource {
"beta": {
Description: "",
Type: schema.TypeBool,
Default: false,
Optional: true,
Default: false,
Optional: true,
},
"display_label": {
Description: "Name of the application to display",
Expand All @@ -54,63 +52,63 @@ func resourceApplication() *schema.Resource {
"learn_more": {
Description: "",
Type: schema.TypeString,
Optional: true,
Optional: true,
},
"constant_attributes":{
Description: "",
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
"constant_attributes": {
Description: "",
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name":{
Type: schema.TypeString,
"name": {
Type: schema.TypeString,
Required: true,
},
"value":{
Type: schema.TypeString,
"value": {
Type: schema.TypeString,
Required: true,
},
"read_only":{
Type: schema.TypeBool,
"read_only": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Default: false,
},
"required":{
Type: schema.TypeBool,
"required": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Default: false,
},
"visible":{
Type: schema.TypeBool,
"visible": {
Type: schema.TypeBool,
Optional: true,
Default: true,
Default: true,
},
},
},
},
"idp_certificate":{
"idp_certificate": {
Description: "",
Type: schema.TypeString,
Required: true,
Sensitive: true,
},
"idp_entity_id":{
"idp_entity_id": {
Description: "",
Type: schema.TypeString,
Required: true,
},
"idp_private_key":{
"idp_private_key": {
Description: "",
Type: schema.TypeString,
Required: true,
Sensitive: true,
Sensitive: true,
},
"sp_entity_id":{
"sp_entity_id": {
Description: "",
Type: schema.TypeString,
Required: true,
},
"acs_url":{
"acs_url": {
Description: "",
Type: schema.TypeString,
Required: true,
Expand Down Expand Up @@ -166,7 +164,7 @@ func resourceApplicationRead(d *schema.ResourceData, meta interface{}) error {
if err := d.Set("sso_url", res.SsoUrl); err != nil {
return err
}

if res.Id != "" {
log.Println("[INFO] response ID is ", res.Id)
orgId := configv1.DefaultHeader["x-org-id"]
Expand Down Expand Up @@ -222,70 +220,67 @@ func generateApplicationPayload(d *schema.ResourceData) jcapiv1.Application {
data := data_raw.(map[string]interface{})
constant := jcapiv1.ApplicationConfigConstantAttributesValue{}

constant.Name = data["name"].([]interface{})[0].(string)
constant.Value = data["value"].([]interface{})[0].(string)
constant.ReadOnly= data["read_only"].([]interface{})[0].(bool)
constant.Required = data["required"].([]interface{})[0].(bool)
constant.Visible = data["visible"].([]interface{})[0].(bool)
constant.Name = data["name"].(string)
constant.Value = data["value"].(string)
constant.ReadOnly = data["read_only"].(bool)
constant.Required = data["required"].(bool)
constant.Visible = data["visible"].(bool)
constants = append(constants, constant)
}
return jcapiv1.Application{
// TODO clearify if previous Active: true is translated to Beta: false
// Active: true,
Beta: d.Get("beta").(bool),
Name: d.Get("name").(string),
DisplayLabel: d.Get("display_label").(string),
SsoUrl: d.Get("sso_url").(string),
Config: &jcapiv1.ApplicationConfig{
AcsUrl: &jcapiv1.ApplicationConfigAcsUrl{
Type_:"text",
Label:"ACS Url:",
Value:d.Get("acs_url").(string),
Required:true,
Visible:true,
ReadOnly:false,
Position:4,
Type_: "text",
Label: "ACS Url:",
Value: d.Get("acs_url").(string),
Required: true,
Visible: true,
ReadOnly: false,
Position: 4,
},
ConstantAttributes: &jcapiv1.ApplicationConfigConstantAttributes{
Value: constants,
},
DatabaseAttributes: &jcapiv1.ApplicationConfigDatabaseAttributes{},
IdpCertificate:&jcapiv1.ApplicationConfigAcsUrl{
Type_:"file",
Label:"IdP Certificate:",
Value:d.Get("idp_certificate").(string),
Required:true,
Visible:true,
ReadOnly:false,
Position:2,
IdpCertificate: &jcapiv1.ApplicationConfigAcsUrl{
Type_: "file",
Label: "IdP Certificate:",
Value: d.Get("idp_certificate").(string),
Required: true,
Visible: true,
ReadOnly: false,
Position: 2,
},
IdpEntityId:&jcapiv1.ApplicationConfigAcsUrl{
Type_:"text",
Label:"IdP Entity ID:",
Value:d.Get("idp_entity_id").(string),
Required:true,
Visible:true,
ReadOnly:false,
Position:0,
IdpEntityId: &jcapiv1.ApplicationConfigAcsUrl{
Type_: "text",
Label: "IdP Entity ID:",
Value: d.Get("idp_entity_id").(string),
Required: true,
Visible: true,
ReadOnly: false,
Position: 0,
},
IdpPrivateKey:&jcapiv1.ApplicationConfigAcsUrl{
Type_:"file",
Label:"IdP Private Key:",
Value:d.Get("idp_private_key").(string),
Required:true,
Visible:true,
ReadOnly:false,
Position:1,
IdpPrivateKey: &jcapiv1.ApplicationConfigAcsUrl{
Type_: "file",
Label: "IdP Private Key:",
Value: d.Get("idp_private_key").(string),
Required: true,
Visible: true,
ReadOnly: false,
Position: 1,
},
SpEntityId:&jcapiv1.ApplicationConfigAcsUrl{
Type_:"text",
Label:"SP Entity ID:",
Value:d.Get("sp_entity_id").(string),
Required:true,
Visible:true,
ReadOnly:false,
Position:4,
SpEntityId: &jcapiv1.ApplicationConfigAcsUrl{
Type_: "text",
Label: "SP Entity ID:",
Value: d.Get("sp_entity_id").(string),
Required: true,
Visible: true,
ReadOnly: false,
Position: 4,
},
},
}
}
}
36 changes: 32 additions & 4 deletions jumpcloud/resource_application_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,59 @@ func Test_resourceApplication(t *testing.T) {
Steps: []resource.TestStep{
// Create step
{
Config: testApplicationConfig(randSuffix, "test_aws_account"),
Config: testApplicationConfig(randSuffix, "test_aws_account", "test_attribute_value"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(fullResourceName, "display_label", "test_aws_account"),
resource.TestCheckResourceAttr(fullResourceName, "constant_attributes.0.name", "test_attribute_name"),
resource.TestCheckResourceAttr(fullResourceName, "constant_attributes.0.value", "test_attribute_value"),
resource.TestCheckResourceAttr(fullResourceName, "constant_attributes.0.read_only", "false"),
resource.TestCheckResourceAttr(fullResourceName, "constant_attributes.0.required", "false"),
resource.TestCheckResourceAttr(fullResourceName, "constant_attributes.0.visible", "true"),
),
},
userImportStep(fullResourceName),
// Update Step
{
Config: testApplicationConfig(randSuffix, "test_aws_account_updated"),
Config: testApplicationConfig(randSuffix, "test_aws_account_updated", "updated_test_value"),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr(fullResourceName, "display_label", "test_aws_account_updated"),
resource.TestCheckResourceAttr(fullResourceName, "constant_attributes.0.name", "test_attribute_name"),
resource.TestCheckResourceAttr(fullResourceName, "constant_attributes.0.value", "updated_test_value"),
resource.TestCheckResourceAttr(fullResourceName, "constant_attributes.0.read_only", "false"),
resource.TestCheckResourceAttr(fullResourceName, "constant_attributes.0.required", "false"),
resource.TestCheckResourceAttr(fullResourceName, "constant_attributes.0.visible", "true"),
),
},
userImportStep(fullResourceName),
},
})
}

func testApplicationConfig(randSuffix string, displayLabel string) string {
// testApplicationConfig generates the Terraform configuration for testing
func testApplicationConfig(randSuffix string, displayLabel string, constantAttrValue string) string {
return fmt.Sprintf(`
resource "jumpcloud_application" "example_app" {
display_label = "%s"
sso_url = "https://sso.jumpcloud.com/saml2/example-application_%s"
saml_role_attribute = "arn:aws:iam::AWS_ACCOUNT_ID:role/MY_ROLE,arn:aws:iam::AWS_ACCOUNT_ID:saml-provider/MY_SAML_PROVIDER"
aws_session_duration = 432000
constant_attributes {
name = "test_attribute_name"
value = "%s"
read_only = false
required = false
visible = true
}
}
`, displayLabel, randSuffix, constantAttrValue)
}
`, displayLabel, randSuffix)

// userImportStep is used to test resource import functionality
func userImportStep(resourceName string) resource.TestStep {
return resource.TestStep{
ResourceName: resourceName,
ImportState: true,
ImportStateVerify: true,
}
}

0 comments on commit a735daf

Please sign in to comment.