-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #71 from ehaligow/ehaligow-patch-chassis-power
PATCH on Chassis/Power
- Loading branch information
Showing
9 changed files
with
258 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package rest | ||
|
||
import ( | ||
"devicemanager/config" | ||
"devicemanager/rest/redfish" | ||
"fmt" | ||
"github.com/kataras/iris/v12" | ||
"github.com/kataras/iris/v12/context" | ||
"github.com/sirupsen/logrus" | ||
"io" | ||
"net/http" | ||
) | ||
|
||
type patchPowerHandler struct { | ||
cfg config.Config | ||
} | ||
|
||
func (r *patchPowerHandler) handle(ctx iris.Context) { | ||
if ctx.GetHeader("Authorization") == "" { | ||
noValidAuthError := "No valid authorization" | ||
ctx.StatusCode(http.StatusUnauthorized) | ||
logrus.Error(noValidAuthError) | ||
ctx.WriteString(noValidAuthError) | ||
return | ||
} | ||
|
||
var reqInfo redfish.RequestInformation | ||
|
||
// Get information from the request. | ||
err := ctx.ReadJSON(&reqInfo) | ||
if err != nil { | ||
missingInfoError := "Unable to retrieve information from a request: " + err.Error() | ||
logrus.Error(missingInfoError) | ||
ctx.StatusCode(http.StatusBadRequest) | ||
ctx.WriteString(missingInfoError) | ||
return | ||
} | ||
|
||
httpClient := redfish.NewHttpClient(r.cfg).WithBasicAuth(reqInfo.Username, string(reqInfo.Password)) | ||
uri := fmt.Sprintf("https://%s%s", reqInfo.Host, ctx.Request().RequestURI) | ||
|
||
response, err := httpClient.Patch(uri, reqInfo.Body) | ||
if err != nil { | ||
errorMessage := fmt.Sprintf("PATCH action failed on %s due to: %s", uri, err.Error()) | ||
logrus.Error(errorMessage) | ||
if response == nil { | ||
ctx.StatusCode(http.StatusInternalServerError) | ||
ctx.WriteString(errorMessage) | ||
return | ||
} | ||
} | ||
|
||
defer response.Body.Close() | ||
body, err := io.ReadAll(response.Body) | ||
if err != nil { | ||
errorMessage := "Error while reading response body: " + err.Error() | ||
logrus.Error(errorMessage) | ||
ctx.StatusCode(http.StatusInternalServerError) | ||
ctx.WriteString(errorMessage) | ||
return | ||
} | ||
|
||
ctx.StatusCode(response.StatusCode) | ||
ctx.Write(body) | ||
} | ||
|
||
func newPatchPowerHandler(cfg config.Config) context.Handler { | ||
return (&patchPowerHandler{ | ||
cfg: cfg, | ||
}).handle | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
package chassis | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/ODIM-Project/ODIM/lib-utilities/common" | ||
chassisproto "github.com/ODIM-Project/ODIM/lib-utilities/proto/chassis" | ||
"github.com/ODIM-Project/ODIM/lib-utilities/response" | ||
"github.com/ODIM-Project/ODIM/svc-systems/scommon" | ||
"github.com/ODIM-Project/ODIM/svc-systems/smodel" | ||
"net/http" | ||
"strings" | ||
) | ||
|
||
func (p *PluginContact) UpdateChassisResource(req *chassisproto.UpdateChassisResourceRequest) response.RPC { | ||
var resp response.RPC | ||
|
||
urlData := strings.Split(req.URL, "/") | ||
var tableName string | ||
if req.URL == "" { | ||
resourceName := urlData[len(urlData)-1] | ||
tableName = common.ChassisResource[resourceName] | ||
} else { | ||
tableName = urlData[len(urlData)-2] | ||
} | ||
|
||
uuid := strings.SplitN(tableName, ".", 2) | ||
target, err := smodel.GetTarget(uuid[0]) | ||
if err != nil { | ||
return common.GeneralError(http.StatusNotFound, response.ResourceNotFound, err.Error(), []interface{}{"Chassis", uuid[0]}, nil) | ||
} | ||
decryptedPasswordByte, error := p.DecryptPassword(target.Password) | ||
if error != nil { | ||
errorMessage := "error while trying to decrypt device password: " + err.Error() | ||
return common.GeneralError(http.StatusInternalServerError, response.InternalError, errorMessage, []interface{}{"Chassis", urlData}, nil) | ||
} | ||
|
||
target.PostBody = req.RequestBody | ||
target.Password = decryptedPasswordByte | ||
|
||
plugin, err := smodel.GetPluginData(target.PluginID) | ||
if err != nil { | ||
errorMessage := "error while trying to get plugin data" | ||
return common.GeneralError(http.StatusInternalServerError, response.InternalError, errorMessage, nil, nil) | ||
} | ||
|
||
var contactRequest scommon.PluginContactRequest | ||
contactRequest.ContactClient = p.ContactClient | ||
contactRequest.Plugin = plugin | ||
contactRequest.HTTPMethodType = http.MethodPatch | ||
contactRequest.OID = fmt.Sprintf("/ODIM/v1/Chassis/%s/Power", uuid[1]) | ||
contactRequest.BasicAuth = map[string]string{ | ||
"UserName": plugin.Username, | ||
"Password": string(plugin.Password), | ||
} | ||
contactRequest.DeviceInfo = target | ||
|
||
body, _, getResponse, contactErr := scommon.ContactPlugin(contactRequest, "error while trying patch Chassis/Power") | ||
if contactErr != nil { | ||
return common.GeneralError(getResponse.StatusCode, getResponse.StatusMessage, contactErr.Error(), nil, nil) | ||
} | ||
|
||
var resource map[string]interface{} | ||
json.Unmarshal(body, &resource) | ||
resp.Body = resource | ||
resp.StatusCode = http.StatusOK | ||
resp.StatusMessage = response.Success | ||
return resp | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters