mc | e702840 | 2019-07-18 04:10:01 +0000 | [diff] [blame] | 1 | // Copyright 2018 Open Networking Foundation |
| 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
| 14 | |
| 15 | package main |
| 16 | |
| 17 | import ( |
| 18 | "net/http" |
| 19 | "fmt" |
| 20 | "encoding/json" |
| 21 | "regexp" |
| 22 | "strings" |
| 23 | "io/ioutil" |
| 24 | ) |
| 25 | |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 26 | func (s *Server) get_status(ip string, service string) (rtn bool, data []string) { |
mc | e702840 | 2019-07-18 04:10:01 +0000 | [diff] [blame] | 27 | rtn = false |
| 28 | |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 29 | uri := s.devicemap[ip].Protocol + "://"+ip + REDFISH_ROOT + service |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 30 | fmt.Printf("%q", uri) |
mc | e702840 | 2019-07-18 04:10:01 +0000 | [diff] [blame] | 31 | resp, err := http.Get(uri) |
| 32 | if err != nil { |
| 33 | fmt.Println(err) |
| 34 | return |
| 35 | } |
| 36 | body := make(map[string]interface{}) |
| 37 | json.NewDecoder(resp.Body).Decode(&body) |
| 38 | resp.Body.Close() |
| 39 | |
| 40 | if members, ok := body["Members"]; ok { |
| 41 | re := regexp.MustCompile(`\[([^\[\]]*)\]`) |
| 42 | memberstr := fmt.Sprintf("%v", members) |
| 43 | matches := re.FindAllString(memberstr, -1) |
| 44 | for _, match := range matches { |
| 45 | m := strings.Trim(match, "[]") |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 46 | uri = s.devicemap[ip].Protocol +"://"+ip + strings.TrimPrefix(m, "@odata.id:") |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame] | 47 | fmt.Println("Printing URI") |
| 48 | fmt.Println(uri) |
mc | e702840 | 2019-07-18 04:10:01 +0000 | [diff] [blame] | 49 | resp, err = http.Get(uri) |
| 50 | if err != nil { |
| 51 | fmt.Println(err) |
| 52 | } else { |
| 53 | b, err := ioutil.ReadAll(resp.Body) |
| 54 | if err != nil { |
| 55 | fmt.Println(err) |
| 56 | } else { |
| 57 | data = append(data, string(b)) |
| 58 | rtn = true |
| 59 | } |
mc | 862dad0 | 2019-08-06 20:52:51 +0000 | [diff] [blame] | 60 | defer resp.Body.Close() |
mc | e702840 | 2019-07-18 04:10:01 +0000 | [diff] [blame] | 61 | } |
mc | e702840 | 2019-07-18 04:10:01 +0000 | [diff] [blame] | 62 | } |
| 63 | } |
| 64 | return |
| 65 | } |