|
1 | 1 | package main |
2 | 2 |
|
3 | 3 | import ( |
| 4 | + "fmt" |
4 | 5 | "net/http" |
5 | 6 | "strings" |
6 | 7 |
|
@@ -51,29 +52,54 @@ func MetaDataHandler(smd smdclient.SMDClientInterface, store cistore.Store) http |
51 | 52 | var err error |
52 | 53 | // If this request includes an id, it can be interrpreted as an impersonation request |
53 | 54 | if urlId == "" { |
| 55 | + log.Debug().Msg("no id specified in request, attempting to identify based on requesting IP") |
54 | 56 | ip := getActualRequestIP(r) |
| 57 | + log.Debug().Msgf("requesting IP is: %s", ip) |
55 | 58 | // Get the component information from the SMD client |
56 | 59 | id, err = smd.IDfromIP(ip) |
57 | 60 | if err != nil { |
58 | | - log.Print(err) |
59 | | - http.Error(w, "Failed to retrieve node ID from IP address", http.StatusUnprocessableEntity) |
| 61 | + log.Printf("did not find id from ip %s: %v", ip, err) |
| 62 | + w.WriteHeader(http.StatusUnprocessableEntity) |
60 | 63 | return |
61 | 64 | } else { |
62 | 65 | log.Printf("xname %s with ip %s found\n", id, ip) |
63 | 66 | } |
| 67 | + } else { |
| 68 | + id = urlId |
64 | 69 | } |
65 | 70 | log.Debug().Msgf("Getting metadata for id: %s", id) |
66 | 71 | smdComponent, err := smd.ComponentInformation(id) |
67 | 72 | if err != nil { |
68 | | - log.Debug().Msgf("Failed to get component information for %s: %s", id, err) |
69 | | - // If the component information is not available, return a 404 |
70 | | - http.Error(w, "Node not found in SMD. Instance-data not available", http.StatusNotFound) |
| 73 | + if esr, ok := err.(smdclient.ErrSMDResponse); ok { |
| 74 | + var msg string |
| 75 | + var status int |
| 76 | + switch esr.HTTPResponse.StatusCode { |
| 77 | + case http.StatusNotFound: |
| 78 | + msg = fmt.Sprintf("node %s not found in SMD", id) |
| 79 | + status = http.StatusNotFound |
| 80 | + default: |
| 81 | + msg = fmt.Sprintf("failed to get component information for node %s: %v", id, err) |
| 82 | + status = http.StatusInternalServerError |
| 83 | + } |
| 84 | + http.Error(w, msg, status) |
| 85 | + } else { |
| 86 | + log.Debug().Msgf("failed to get component information for node %s: %s", id, err) |
| 87 | + http.Error(w, fmt.Sprintf("internal error occurred fetching component information for node %s", id), http.StatusInternalServerError) |
| 88 | + } |
71 | 89 | return |
72 | 90 | } |
73 | 91 | groups, err := smd.GroupMembership(id) |
74 | 92 | if err != nil { |
75 | | - // If the group information is not available, return an empty list |
76 | | - groups = []string{} |
| 93 | + if esr, ok := err.(smdclient.ErrSMDResponse); ok { |
| 94 | + switch esr.HTTPResponse.StatusCode { |
| 95 | + case http.StatusBadRequest: |
| 96 | + http.Error(w, fmt.Sprintf("%s is not a valid xname for SMD", id), http.StatusBadRequest) |
| 97 | + return |
| 98 | + default: |
| 99 | + // If the group information is not available, return an empty list |
| 100 | + groups = []string{} |
| 101 | + } |
| 102 | + } |
77 | 103 | } |
78 | 104 | bootIP, err := smd.IPfromID(id) |
79 | 105 | if err != nil { |
|
0 commit comments