Skip to content

Commit

Permalink
Merge pull request #33 from Traceableai/user-att-bug
Browse files Browse the repository at this point in the history
ENG-46219 fix user att bug
  • Loading branch information
adityaKumar9006 authored Jun 26, 2024
2 parents 3a52d3a + 3b97bce commit 0085792
Show file tree
Hide file tree
Showing 14 changed files with 295 additions and 195 deletions.
38 changes: 0 additions & 38 deletions docs/resources/api_exclusion_rule.md

This file was deleted.

1 change: 1 addition & 0 deletions docs/resources/user_attribution_rule_basic_auth.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ resource "traceable_user_attribution_rule_basic_auth" "test1" {

### Optional

- `disabled` (Boolean) Flag to enable or disable the rule
- `environment` (String) environment
- `url_regex` (String) url regex

Expand Down
1 change: 1 addition & 0 deletions docs/resources/user_attribution_rule_custom_json.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ resource "traceable_user_attribution_rule_custom_json" "test5" {

### Optional

- `disabled` (Boolean) Flag to enable or disable the rule
- `environment` (String) environement of rule
- `url_regex` (String) url regex
- `user_id_json` (String) user id json
Expand Down
1 change: 1 addition & 0 deletions docs/resources/user_attribution_rule_custom_token.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ resource "traceable_user_attribution_rule_custom_token" "test6" {

### Optional

- `disabled` (Boolean) Flag to enable or disable the rule
- `environment` (String) environement of rule
- `url_regex` (String) url regex

Expand Down
1 change: 1 addition & 0 deletions docs/resources/user_attribution_rule_jwt_authentication.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ resource "traceable_user_attribution_rule_jwt_authentication" "test3" {
### Optional

- `auth_type` (String) auth type of the user attribution rule
- `disabled` (Boolean) Flag to enable or disable the rule
- `environment` (String) environment
- `token_capture_group` (String) token capture group
- `url_regex` (String) url regex
Expand Down
1 change: 1 addition & 0 deletions docs/resources/user_attribution_rule_req_header.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ resource "traceable_user_attribution_rule_req_header" "test2" {
### Optional

- `auth_type` (String) auth type of the user attribution rule
- `disabled` (Boolean) Flag to enable or disable the rule
- `environment` (String) environment
- `role_location_regex_capture_group` (String) user role location regex capture group
- `url_regex` (String) url regex
Expand Down
1 change: 1 addition & 0 deletions docs/resources/user_attribution_rule_response_body.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ resource "traceable_user_attribution_rule_response_body" "test4" {
### Optional

- `auth_type` (String) auth type of the user attribution rule
- `disabled` (Boolean) Flag to enable or disable the rule
- `user_role_location_json_path` (String) user role location json path

### Read-Only
Expand Down
2 changes: 1 addition & 1 deletion provider/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func Provider() *schema.Provider {
"traceable_notification_rule_threat_actor_status": resourceNotificationRuleThreatActorStatusChange(),
"traceable_notification_rule_actor_severity_change": resourceNotificationRuleActorSeverityChange(),
"traceable_api_naming_rule": resourceApiNamingRule(),
"traceable_api_exclusion_rule": resourceApiExclusionRule(),
// "traceable_api_exclusion_rule": resourceApiExclusionRule(),
"traceable_label_creation_rule": resourceLabelCreationRule(),
"traceable_agent_token": resourceAgentToken(),

Expand Down
35 changes: 27 additions & 8 deletions provider/resource_user_attribution_basic_auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,20 +35,33 @@ func resourceUserAttributionBasicAuthRule() *schema.Resource {
Description: "url regex",
Optional: true,
},
"disabled": {
Type: schema.TypeBool,
Description: "Flag to enable or disable the rule",
Optional: true,
Default: false,
},
"category": {
Type: schema.TypeString,
Description: "Type of user attribution rule",
Optional: true,
Default: "BASIC_AUTH",
},
},
}
}

func resourceUserAttributionRuleBasicAuthCreate(d *schema.ResourceData, meta interface{}) error {
name := d.Get("name").(string)
category := d.Get("category").(string)

scopeType:=d.Get("scope_type").(string)

var query string
if scopeType == "SYSTEM_WIDE" {
query = fmt.Sprintf(`mutation {
createUserAttributionRule(
input: {name: "%s", type: BASIC_AUTH, scopeType: %s}
input: {name: "%s", type: %s, scopeType: %s}
) {
results {
id
Expand All @@ -58,7 +71,7 @@ func resourceUserAttributionRuleBasicAuthCreate(d *schema.ResourceData, meta int
}
total
}
}`,name,scopeType)
}`,name,category,scopeType)
} else if scopeType== "CUSTOM" {
environment:=d.Get("environment").(string)
url_regex:=d.Get("url_regex").(string)
Expand All @@ -73,7 +86,7 @@ func resourceUserAttributionRuleBasicAuthCreate(d *schema.ResourceData, meta int
}
query = fmt.Sprintf(`mutation {
createUserAttributionRule(
input: {name: "%s", type: BASIC_AUTH, scopeType: CUSTOM, %s}
input: {name: "%s", type: %s, scopeType: CUSTOM, %s}
) {
results {
id
Expand All @@ -84,7 +97,7 @@ func resourceUserAttributionRuleBasicAuthCreate(d *schema.ResourceData, meta int
}
total
}
}`,name,scopedQuery)
}`,name,category,scopedQuery)
}else{
return fmt.Errorf("Expected values are CUSTOM or SYSTEM_WIDE for user attribution scope type")
}
Expand Down Expand Up @@ -126,6 +139,10 @@ func resourceUserAttributionRuleBasicAuthRead(d *schema.ResourceData, meta inter
}
log.Printf("fetching from read %s",ruleDetails)
name:=ruleDetails["name"].(string)
category:=ruleDetails["type"].(string)
disabled:=ruleDetails["disabled"].(bool)
d.Set("disabled",disabled)
d.Set("category",category)
scopeType:=ruleDetails["scopeType"]
d.Set("name",name)
if scopeType=="SYSTEM_WIDE"{
Expand All @@ -148,7 +165,9 @@ func resourceUserAttributionRuleBasicAuthRead(d *schema.ResourceData, meta inter
func resourceUserAttributionRuleBasicAuthUpdate(d *schema.ResourceData, meta interface{}) error {
id:=d.Id()
name := d.Get("name").(string)
disabled := d.Get("disabled").(bool)
scopeType:=d.Get("scope_type").(string)
category:=d.Get("category").(string)
readQuery:="{userAttributionRules{results{id scopeType rank name type disabled customScope{environmentScopes{environmentName}urlScopes{urlMatchRegex}}}}}"
readQueryResStr, err := executeQuery(readQuery, meta)
if err != nil {
Expand All @@ -167,14 +186,14 @@ func resourceUserAttributionRuleBasicAuthUpdate(d *schema.ResourceData, meta int
if scopeType == "SYSTEM_WIDE" {
query = fmt.Sprintf(`mutation {
updateUserAttributionRule(
rule: {name: "%s", type: BASIC_AUTH,id:"%s",rank:%d, scopeType: %s}
rule: {name: "%s", type: %s,id:"%s",rank:%d,disabled: %t, scopeType: %s}
) {
id
scopeType
rank
name
}
}`,name,id,rank,scopeType)
}`,name,category,id,rank,disabled,scopeType)
} else if scopeType== "CUSTOM" {
environment:=d.Get("environment").(string)
url_regex:=d.Get("url_regex").(string)
Expand All @@ -189,15 +208,15 @@ func resourceUserAttributionRuleBasicAuthUpdate(d *schema.ResourceData, meta int
}
query = fmt.Sprintf(`mutation {
updateUserAttributionRule(
rule: {name: "%s", type: BASIC_AUTH,id:"%s",rank:%d, scopeType: CUSTOM, %s}
rule: {name: "%s", type: %s,id:"%s",rank:%d,disabled: %t scopeType: CUSTOM, %s}
) {
id
scopeType
rank
name
type
}
}`,name,id,rank,scopedQuery)
}`,name,category,id,rank,disabled,scopedQuery)
}else{
return fmt.Errorf("Expected values are CUSTOM or SYSTEM_WIDE for user attribution scope type")
}
Expand Down
50 changes: 35 additions & 15 deletions provider/resource_user_attribution_custom_json.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,12 +50,25 @@ func resourceUserAttributionCustomJsonRule() *schema.Resource {
Description: "user role json",
Optional: true,
},
"disabled": {
Type: schema.TypeBool,
Description: "Flag to enable or disable the rule",
Optional: true,
Default: false,
},
"category": {
Type: schema.TypeString,
Description: "Type of user attribution rule",
Optional: true,
Default: "CUSTOM_JSON",
},
},
}
}

func resourceUserAttributionRuleCustomJsonCreate(d *schema.ResourceData, meta interface{}) error {
name := d.Get("name").(string)
category := d.Get("category").(string)
scope_type := d.Get("scope_type").(string)
environment := d.Get("environment").(string)
url_regex:=d.Get("url_regex").(string)
Expand Down Expand Up @@ -95,7 +108,7 @@ func resourceUserAttributionRuleCustomJsonCreate(d *schema.ResourceData, meta in
createUserAttributionRule(
input: {
name: "%s",
type: CUSTOM_JSON,
type: %s,
scopeType: %s,
%s
%s
Expand All @@ -110,7 +123,7 @@ func resourceUserAttributionRuleCustomJsonCreate(d *schema.ResourceData, meta in
}
total
}
}`,name,scope_type,customJsonString,customScopeString)
}`,name,category,scope_type,customJsonString,customScopeString)

var response map[string]interface{}
responseStr, err := executeQuery(query, meta)
Expand Down Expand Up @@ -151,8 +164,11 @@ func resourceUserAttributionRuleCustomJsonRead(d *schema.ResourceData, meta inte
log.Printf("fetching from read %s",ruleDetails)
name:=ruleDetails["name"].(string)
scopeType:=ruleDetails["scopeType"].(string)
category:=ruleDetails["type"].(string)
d.Set("category",category)
d.Set("name",name)

disabled:=ruleDetails["disabled"].(bool)
d.Set("disabled",disabled)
if scopeType=="SYSTEM_WIDE"{
d.Set("scope_type", "SYSTEM_WIDE")
// d.Set("url_regex",nil)
Expand All @@ -170,17 +186,19 @@ func resourceUserAttributionRuleCustomJsonRead(d *schema.ResourceData, meta inte
// d.Set("url_regex",nil)
}
}
customJsonDetails:=ruleDetails["customJson"]
if customJsonDetails!=nil{

customJsonDetails:=ruleDetails["customJson"].(map[string]interface{})

authTypeJson,err:=json.Marshal(customJsonDetails["authTypeJson"])
userIdJson,err:=json.Marshal(customJsonDetails["userIdJson"])
userRoleJson,err:=json.Marshal(customJsonDetails["userRoleJson"])
authTypeJson,_:=json.Marshal(customJsonDetails.(map[string]interface{})["authTypeJson"])
userIdJson,_:=json.Marshal(customJsonDetails.(map[string]interface{})["userIdJson"])
userRoleJson,_:=json.Marshal(customJsonDetails.(map[string]interface{})["userRoleJson"])

d.Set("auth_type_json",authTypeJson)
d.Set("user_id_json",userIdJson)
d.Set("user_role_json",userRoleJson)

d.Set("auth_type_json",authTypeJson)
d.Set("user_id_json",userIdJson)
d.Set("user_role_json",userRoleJson)

return nil
}
return nil
}

Expand All @@ -200,8 +218,9 @@ func resourceUserAttributionRuleCustomJsonUpdate(d *schema.ResourceData, meta in
return nil
}
rank:=int(readRuleDetails["rank"].(float64))

category:=d.Get("category").(string)
name := d.Get("name").(string)
disabled := d.Get("disabled").(bool)
scope_type := d.Get("scope_type").(string)
environment := d.Get("environment").(string)
url_regex:=d.Get("url_regex").(string)
Expand Down Expand Up @@ -242,7 +261,8 @@ func resourceUserAttributionRuleCustomJsonUpdate(d *schema.ResourceData, meta in
id:"%s",
rank:%d
name: "%s",
type: CUSTOM_JSON,
disabled: %t,
type: %s,
scopeType: %s,
%s
%s
Expand All @@ -254,7 +274,7 @@ func resourceUserAttributionRuleCustomJsonUpdate(d *schema.ResourceData, meta in
name
type
}
}`,id,rank,name,scope_type,customJsonString,customScopeString)
}`,id,rank,name,disabled,category,scope_type,customJsonString,customScopeString)


var response map[string]interface{}
Expand Down
Loading

0 comments on commit 0085792

Please sign in to comment.