Skip to content

Commit

Permalink
Add crypto adapters and domains attach support
Browse files Browse the repository at this point in the history
Signed-off-by: zhangli <[email protected]>
  • Loading branch information
bjzhangl committed Oct 30, 2023
1 parent 6bb10a7 commit 49c7932
Show file tree
Hide file tree
Showing 6 changed files with 251 additions and 10 deletions.
81 changes: 81 additions & 0 deletions pkg/zhmcclient/fakes/lpar.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

81 changes: 81 additions & 0 deletions pkg/zhmcclient/fakes/zhmc.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 36 additions & 3 deletions pkg/zhmcclient/lpar.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ type LparAPI interface {
FetchAsciiConsoleURI(lparURI string, request *AsciiConsoleURIPayload) (*AsciiConsoleURIResponse, int, *HmcError)

GetEnergyDetailsforLPAR(lparURI string, props *EnergyRequestPayload) (uint64, int, *HmcError)

AttachCryptoToPartition(lparURI string, request *CryptoConfig) (int, *HmcError)
}

type LparManager struct {
Expand Down Expand Up @@ -625,13 +627,13 @@ func (m *LparManager) FetchAsciiConsoleURI(lparURI string, request *AsciiConsole
"timestamp": 1680408593302
}]
}
*/
*/
func (m *LparManager) GetEnergyDetailsforLPAR(lparURI string, props *EnergyRequestPayload) (uint64, int, *HmcError) {
requestUrl := m.client.CloneEndpointURL()

requestUrl.Path = path.Join(requestUrl.Path, lparURI, "/operations", "/get-historical-sustainability-data")
logger.Info("Request URL:" + string(requestUrl.Path) + " Method:" + http.MethodPost + " props" + fmt.Sprint(props))
logger.Info("Request URL:" + string(requestUrl.Path) + " Method:" + http.MethodPost + " props" + fmt.Sprint(props))
status, responseBody, err := m.client.ExecuteRequest(http.MethodPost, requestUrl, props, "")

if err != nil {
Expand All @@ -657,3 +659,34 @@ func (m *LparManager) GetEnergyDetailsforLPAR(lparURI string, props *EnergyReque
errorResponseBody := GenerateErrorFromResponse(responseBody)
return 0, status, errorResponseBody
}

func (m *LparManager) AttachCryptoToPartition(lparURI string, request *CryptoConfig) (int, *HmcError) {
requestUrl := m.client.CloneEndpointURL()
requestUrl.Path = path.Join(requestUrl.Path, lparURI, "/operations/increase-crypto-configuration")

logger.Info(fmt.Sprintf("Request URL: %v, Method: %v", requestUrl, http.MethodPost))
logger.Info(fmt.Sprintf("request: %v", request))
status, responseBody, err := m.client.ExecuteRequest(http.MethodPost, requestUrl, request, "")

if err != nil {
logger.Error("error on attach crypto adapters and domains to partition",
zap.String("request url", fmt.Sprint(lparURI)),
zap.String("method", http.MethodPost),
zap.String("status", fmt.Sprint(status)),
zap.Error(fmt.Errorf("%v", err)))
return status, err
}

if status == http.StatusNoContent {
logger.Info(fmt.Sprintf("Response: attach crypto adapters and domains to partition successfull, request url: %v, method: %v, status: %v", lparURI, http.MethodPost, status))
return status, nil
}

errorResponseBody := GenerateErrorFromResponse(responseBody)
logger.Error("error attaching crypto adapters and domains to partition",
zap.String("request url", fmt.Sprint(lparURI)),
zap.String("method", http.MethodPost),
zap.String("status: ", fmt.Sprint(status)),
zap.Error(fmt.Errorf("%v", errorResponseBody)))
return status, errorResponseBody
}
46 changes: 44 additions & 2 deletions pkg/zhmcclient/lpar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ var _ = Describe("LPAR", func() {

Describe("GetEnergyDetailsforLPAR", func() {
var (
bytes []byte
bytes []byte
)

BeforeEach(func() {
Expand All @@ -149,7 +149,7 @@ var _ = Describe("LPAR", func() {
fakeClient.CloneEndpointURLReturns(url)
fakeClient.ExecuteRequestReturns(http.StatusOK, bytes, nil)
props := &EnergyRequestPayload{
Range: "last-day",
Range: "last-day",
Resolution: "fifteen-minutes",
}
rets, _, err := manager.GetEnergyDetailsforLPAR(lparid, props)
Expand Down Expand Up @@ -680,4 +680,46 @@ var _ = Describe("LPAR", func() {
})
})

Describe("AttachCryptoToPartition", func() {
var cryptoConfig *CryptoConfig
BeforeEach(func() {
cryptoConfig = &CryptoConfig{
CryptoAdapterUris: []string{"uri"},
CryptoDomainConfigurations: []DomainInfo{
{
DomainIdx: 1,
AccessMode: "control",
},
},
}
})
Context("When AttachCryptoToPartition returns correctly", func() {
It("Check the results Succeed", func() {
fakeClient.CloneEndpointURLReturns(url)
fakeClient.ExecuteRequestReturns(http.StatusOK, nil, nil)
rets, _ := manager.AttachCryptoToPartition(lparid, cryptoConfig)
Expect(rets).ToNot(BeNil())
})

})
Context("When AttachCryptoToPartition returns error due to hmcErr", func() {
It("check the error happened", func() {
fakeClient.CloneEndpointURLReturns(url)
fakeClient.ExecuteRequestReturns(http.StatusOK, nil, hmcErr)
rets, err := manager.AttachCryptoToPartition(lparid, cryptoConfig)
Expect(rets).To(Equal(200))
Expect(err).To(Equal(hmcErr))
})
})
Context("When AttachCryptoToPartition returns correctly with status 204", func() {
It("check the response has no content", func() {
fakeClient.CloneEndpointURLReturns(url)
fakeClient.ExecuteRequestReturns(http.StatusNoContent, nil, nil)
rets, err := manager.AttachCryptoToPartition(lparid, cryptoConfig)
Expect(err).To(BeNil())
Expect(rets).To(Equal(204))
})
})
})

})
8 changes: 4 additions & 4 deletions pkg/zhmcclient/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -1029,8 +1029,8 @@ type CryptoConfig struct {
}

type EnergyRequestPayload struct {
Timescale string `json:"timescale,omitempty"`
Type string `json:"type,omitempty"`
Range string `json:"range,omitempty"`
Resolution string `json:"resolution,omitempty"`
Timescale string `json:"timescale,omitempty"`
Type string `json:"type,omitempty"`
Range string `json:"range,omitempty"`
Resolution string `json:"resolution,omitempty"`
}
6 changes: 5 additions & 1 deletion pkg/zhmcclient/zhmc.go
Original file line number Diff line number Diff line change
Expand Up @@ -118,8 +118,12 @@ func (m *ZhmcManager) GetEnergyDetailsforLPAR(lparURI string, props *EnergyReque
return m.lparManager.GetEnergyDetailsforLPAR(lparURI, props)
}

func (m *ZhmcManager) AttachCryptoToPartition(lparURI string, request *CryptoConfig) (int, *HmcError) {
return m.lparManager.AttachCryptoToPartition(lparURI, request)
}

func (m *ZhmcManager) GetLiveEnergyDetailsforLPAR(lparURI string) (uint64, int, *HmcError) {
return m.metricsManager.GetLiveEnergyDetailsforLPAR(lparURI)
return m.metricsManager.GetLiveEnergyDetailsforLPAR(lparURI)
}

// Adapter
Expand Down

0 comments on commit 49c7932

Please sign in to comment.