Skip to content
This repository was archived by the owner on Nov 18, 2024. It is now read-only.

Commit 84e3405

Browse files
authored
Merge pull request #74 from julfur/hmc-logoff-feature
Added HMC logoff feature to avoid REST API idling session
2 parents 8a5f061 + 46b5b3a commit 84e3405

File tree

2 files changed

+36
-3
lines changed

2 files changed

+36
-3
lines changed

hmc/hmc.go

Lines changed: 35 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ type HMC struct {
3737
ManagedSystemOnly bool
3838
Samples int
3939
TagParsers nmon2influxdblib.TagParsers
40+
Token string
4041
}
4142

4243
// Point is a struct to simplify InfluxDB point creation
@@ -87,8 +88,7 @@ func NewHMC(c *cli.Context) *HMC {
8788
hmcURL := fmt.Sprintf("https://"+"%s"+":12443", config.HMCServer)
8889
//initialize new http session
8990
hmc.Session = NewSession(config.HMCUser, config.HMCPassword, hmcURL)
90-
hmc.Session.doLogon()
91-
91+
hmc.Token = hmc.Session.doLogon()
9292
return &hmc
9393
}
9494

@@ -235,8 +235,12 @@ func NewSession(user string, password string, url string) *Session {
235235
return &Session{client: &http.Client{Transport: tr, Jar: jar, Timeout: time.Second * timeout}, User: user, Password: password, url: url}
236236
}
237237

238+
type Token struct {
239+
API string `xml:"X-API-Session"`
240+
}
241+
238242
// doLogon performs the login to the inflxudb instance
239-
func (s *Session) doLogon() {
243+
func (s *Session) doLogon() (string){
240244

241245
authurl := s.url + "/rest/api/web/Logon"
242246

@@ -274,6 +278,34 @@ func (s *Session) doLogon() {
274278
log.Fatalf("HMC authentication error: %s\n", response.Status)
275279
}
276280
}
281+
282+
// store session token
283+
var token Token
284+
bodyBytes, err := ioutil.ReadAll(response.Body)
285+
xml.Unmarshal([]byte(bodyBytes), &token)
286+
// log.Printf("%v", token.API)
287+
return token.API
288+
}
289+
290+
func (s *Session) DoLogoff(token string) {
291+
authurl := s.url + "/rest/api/web/Logon"
292+
request, err := http.NewRequest("DELETE", authurl, nil)
293+
294+
// set request headers
295+
// log.Printf("%v", token)
296+
request.Header.Set("X-API-Session", token)
297+
response, err := s.client.Do(request)
298+
299+
if err != nil {
300+
log.Fatalf("HMC error sending auth request: %v\n", err)
301+
} else {
302+
defer response.Body.Close()
303+
if response.StatusCode != 204 {
304+
log.Fatalf("HMC logoff error: %s\n", response.Status)
305+
} else {
306+
log.Printf("Succesfully logged off")
307+
}
308+
}
277309
}
278310

279311
// PCMLinks store a system and associated partitions links to PCM data

hmc/import.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,4 +286,5 @@ func Import(c *cli.Context) {
286286
}
287287
}
288288
}
289+
hmc.Session.DoLogoff(hmc.Token)
289290
}

0 commit comments

Comments
 (0)