Skip to content

Commit

Permalink
Merge pull request #351 from sangitamohan/master
Browse files Browse the repository at this point in the history
Added check for hourly permissions and version >= 37 before querying …
  • Loading branch information
dcarroll authored Nov 29, 2016
2 parents 95f3519 + 27b1c9c commit a809749
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 9 deletions.
2 changes: 1 addition & 1 deletion apiversion.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ func runApiVersion(cmd *Command, args []string) {
apiVersionNumber = args[0]
apiVersion = "v" + apiVersionNumber
force.Credentials.ApiVersion = apiVersionNumber
ForceSaveLogin(force.Credentials)
ForceSaveLogin(*force.Credentials)
} else if len(args) == 0 {
fmt.Println(apiVersion)
} else {
Expand Down
45 changes: 39 additions & 6 deletions force.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const (
)*/

type Force struct {
Credentials ForceCredentials
Credentials *ForceCredentials
Metadata *ForceMetadata
Partner *ForcePartner
}
Expand All @@ -56,6 +56,8 @@ type ForceCredentials struct {
ApiVersion string
RefreshToken string
ForceEndpoint ForceEndpoint
IsHourly bool
HourlyCheck bool
}

type LoginFault struct {
Expand Down Expand Up @@ -225,7 +227,7 @@ type BundleManifest struct {
Files []ComponentFile
}

func NewForce(creds ForceCredentials) (force *Force) {
func NewForce(creds *ForceCredentials) (force *Force) {
force = new(Force)
force.Credentials = creds
force.Metadata = NewForceMetadata(force)
Expand Down Expand Up @@ -272,9 +274,8 @@ func ForceSoapLogin(endpoint ForceEndpoint, username string, password string) (c
}
instanceUrl := u.Scheme + "://" + u.Host
identity := u.Scheme + "://" + u.Host + "/id/" + orgid + "/" + result.Id
creds = ForceCredentials{AccessToken: result.SessionId, Id: identity, UserId: result.Id, InstanceUrl: instanceUrl, IsCustomEP: endpoint == EndpointCustom, ApiVersion: apiVersionNumber, ForceEndpoint: endpoint}
creds = ForceCredentials{AccessToken: result.SessionId, Id: identity, UserId: result.Id, InstanceUrl: instanceUrl, IsCustomEP: endpoint == EndpointCustom, ApiVersion: apiVersionNumber, ForceEndpoint: endpoint, IsHourly: false, HourlyCheck: false}
LogAuth()

return
}

Expand All @@ -289,7 +290,7 @@ func (f *Force) UpdateCredentials(creds ForceCredentials) {
f.Credentials.InstanceUrl = creds.InstanceUrl
f.Credentials.Scope = creds.Scope
f.Credentials.Id = creds.Id
ForceSaveLogin(f.Credentials)
ForceSaveLogin(*f.Credentials)
}

func (f *Force) refreshTokenURL() string {
Expand Down Expand Up @@ -1210,8 +1211,40 @@ func (f *Force) RetrieveEventLogFile(elfId string) (result string, err error) {
return
}

func (force *Force) setHourlyPerm() {
force.Credentials.HourlyCheck = true
const EventLogFile string = "EventLogFile"
const HourlyEnabledField string = "Sequence"
force.Credentials.IsHourly = false
sobject, err := force.GetSobject(EventLogFile)
if err != nil {
ErrorAndExit(err.Error())
}
fields := ForceSobjectFields(sobject["fields"].([]interface{}))
for _, f := range fields {
field := f.(map[string]interface{})
if field["name"] == HourlyEnabledField {
force.Credentials.IsHourly = true
break
}
}
return
}

func (f *Force) QueryEventLogFiles() (results ForceQueryResult, err error) {
url := fmt.Sprintf("%s/services/data/%s/query/?q=Select+Id,+LogDate,+EventType,+LogFileLength+FROM+EventLogFile+ORDER+BY+LogDate+DESC,+EventType", f.Credentials.InstanceUrl, apiVersion)
if !f.Credentials.HourlyCheck {
f.setHourlyPerm()
}
url := ""
currApi, e := strconv.ParseFloat(f.Credentials.ApiVersion, 64)
if e != nil {
ErrorAndExit(e.Error())
}
if f.Credentials.IsHourly && currApi >= 37.0 {
url = fmt.Sprintf("%s/services/data/%s/query/?q=Select+Id,+LogDate,+EventType,+LogFileLength,+Sequence,+Interval+FROM+EventLogFile+ORDER+BY+LogDate+DESC,+EventType,+Sequence,+Interval", f.Credentials.InstanceUrl, apiVersion)
} else {
url = fmt.Sprintf("%s/services/data/%s/query/?q=Select+Id,+LogDate,+EventType,+LogFileLength+FROM+EventLogFile+ORDER+BY+LogDate+DESC,+EventType", f.Credentials.InstanceUrl, apiVersion)
}
body, err := f.httpGet(url)
if err != nil {
return
Expand Down
2 changes: 1 addition & 1 deletion login.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ func CurrentEndpoint() (endpoint ForceEndpoint, customUrl string, err error) {
}

func ForceSaveLogin(creds ForceCredentials) (username string, err error) {
force := NewForce(creds)
force := NewForce(&creds)
login, err := force.Get(creds.Id)
if err != nil {
return
Expand Down
2 changes: 1 addition & 1 deletion logins.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ func ActiveForce() (force *Force, err error) {
if err != nil {
return
}
force = NewForce(creds)
force = NewForce(&creds)
return
}

Expand Down

0 comments on commit a809749

Please sign in to comment.