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 | |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame^] | 29 | // uri := "https://"+ip + REDFISH_ROOT + service |
| 30 | uri := s.devicemap[ip].protocol+"://"+ip + REDFISH_ROOT + service |
| 31 | fmt.Printf("%q", uri) |
mc | e702840 | 2019-07-18 04:10:01 +0000 | [diff] [blame] | 32 | resp, err := http.Get(uri) |
| 33 | if err != nil { |
| 34 | fmt.Println(err) |
| 35 | return |
| 36 | } |
| 37 | body := make(map[string]interface{}) |
| 38 | json.NewDecoder(resp.Body).Decode(&body) |
| 39 | resp.Body.Close() |
| 40 | |
| 41 | if members, ok := body["Members"]; ok { |
| 42 | re := regexp.MustCompile(`\[([^\[\]]*)\]`) |
| 43 | memberstr := fmt.Sprintf("%v", members) |
| 44 | matches := re.FindAllString(memberstr, -1) |
| 45 | for _, match := range matches { |
| 46 | m := strings.Trim(match, "[]") |
Dinesh Belwalkar | e1e85ad | 2019-07-31 23:06:47 +0000 | [diff] [blame^] | 47 | uri = s.devicemap[ip].protocol +"://"+ip + strings.TrimPrefix(m, "@odata.id:") |
| 48 | fmt.Println("Printing URI") |
| 49 | fmt.Println(uri) |
mc | e702840 | 2019-07-18 04:10:01 +0000 | [diff] [blame] | 50 | resp, err = http.Get(uri) |
| 51 | if err != nil { |
| 52 | fmt.Println(err) |
| 53 | } else { |
| 54 | b, err := ioutil.ReadAll(resp.Body) |
| 55 | if err != nil { |
| 56 | fmt.Println(err) |
| 57 | } else { |
| 58 | data = append(data, string(b)) |
| 59 | rtn = true |
| 60 | } |
| 61 | } |
| 62 | defer resp.Body.Close() |
| 63 | } |
| 64 | } |
| 65 | return |
| 66 | } |