Skip to content

Commit

Permalink
feat: rename appliance/blade/host
Browse files Browse the repository at this point in the history
* feat: add api appliancesRenameById

* feat: add api bladesRenameById

* feat: add api RenameHostById

* feat: change to use PUT method

* feat: error handling

* feat: change the API name from rename to update

* refactor: remove unnecessary code
  • Loading branch information
Meng-20 authored Oct 7, 2024
1 parent 954397e commit 06dbdf1
Show file tree
Hide file tree
Showing 5 changed files with 338 additions and 2 deletions.
107 changes: 107 additions & 0 deletions api/cfm-openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,41 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/statusMessage"
put:
description: Update an appliance with a new id.
operationId: appliancesUpdateById
parameters:
- $ref: "#/components/parameters/applianceId"
- name: newApplianceId
in: query
required: true
schema:
type: string
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/appliance'
"400":
description: Bad Request
content:
application/json:
schema:
$ref: '#/components/schemas/statusMessage'
"404":
description: Not Found
content:
application/json:
schema:
$ref: '#/components/schemas/statusMessage'
"500":
description: Internal Server Error
content:
application/json:
schema:
$ref: '#/components/schemas/statusMessage'

# Memory Appliance Blades
/cfm/v1/appliances/{applianceId}/blades:
Expand Down Expand Up @@ -328,6 +363,42 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/statusMessage"
put:
description: Update a blade with a new id.
operationId: bladesUpdateById
parameters:
- $ref: "#/components/parameters/applianceId"
- $ref: "#/components/parameters/bladeId"
- name: newBladeId
in: query
required: true
schema:
type: string
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/blade'
"400":
description: Bad Request
content:
application/json:
schema:
$ref: '#/components/schemas/statusMessage'
"404":
description: Not Found
content:
application/json:
schema:
$ref: '#/components/schemas/statusMessage'
"500":
description: Internal Server Error
content:
application/json:
schema:
$ref: "#/components/schemas/statusMessage"

# Memory Blade Memory Resource Blocks
/cfm/v1/appliances/{applianceId}/blades/{bladeId}/resources:
Expand Down Expand Up @@ -808,6 +879,42 @@ paths:
application/json:
schema:
$ref: "#/components/schemas/statusMessage"
put:
description: Update a CXL host with a new id.
operationId: hostsUpdateById
parameters:
- $ref: "#/components/parameters/hostId"
- name: newHostId
in: query
required: true
schema:
type: string
responses:
"200":
description: OK
content:
application/json:
schema:
$ref: '#/components/schemas/host'
"400":
description: Bad Request
content:
application/json:
schema:
$ref: '#/components/schemas/statusMessage'
"404":
description: Not Found
content:
application/json:
schema:
$ref: '#/components/schemas/statusMessage'
"500":
description: Internal Server Error
content:
application/json:
schema:
$ref: "#/components/schemas/statusMessage"

# CXL Host Ports
/cfm/v1/hosts/{hostId}/ports:
get:
Expand Down
90 changes: 90 additions & 0 deletions pkg/api/api_default_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,33 @@ func (cfm *CfmApiService) AppliancesPost(ctx context.Context, credentials openap
return openapi.Response(http.StatusCreated, a), nil
}

// AppliancesUpdateById -
func (cfm *CfmApiService) AppliancesUpdateById(ctx context.Context, applianceId string, newApplianceId string) (openapi.ImplResponse, error) {
// Make sure the newApplianceId doesn't exist
_, exist := manager.GetApplianceById(ctx, newApplianceId)
if exist == nil {
err := common.RequestError{
StatusCode: common.StatusApplianceIdDuplicate,
Err: fmt.Errorf("the new name (%s) already exists", newApplianceId),
}
return formatErrorResp(ctx, &err)
}

// Make sure the appliance exists
appliance, err := manager.GetApplianceById(ctx, applianceId)
if err != nil {
return formatErrorResp(ctx, err.(*common.RequestError))
}

//Rename the appliance with the new id
newAppliance, err := manager.RenameAppliance(ctx, appliance, newApplianceId)
if err != nil {
return formatErrorResp(ctx, err.(*common.RequestError))
}

return openapi.Response(http.StatusOK, newAppliance), nil
}

// AppliancesResync -
func (cfm *CfmApiService) AppliancesResyncById(ctx context.Context, applianceId string) (openapi.ImplResponse, error) {
appliance, err := manager.ResyncApplianceById(ctx, applianceId)
Expand Down Expand Up @@ -495,6 +522,41 @@ func (cfm *CfmApiService) BladesGetPorts(ctx context.Context, applianceId string
return openapi.Response(http.StatusOK, response), nil
}

// BladesUpdateById -
func (cfm *CfmApiService) BladesUpdateById(ctx context.Context, applianceId string, bladeId string, newBladeId string) (openapi.ImplResponse, error) {
appliance, err := manager.GetApplianceById(ctx, applianceId)
if err != nil {
return formatErrorResp(ctx, err.(*common.RequestError))
}

// Make sure the bladeId exists
// Get the blade information from the manager level and is used for renaming
blade, err := appliance.GetBladeById(ctx, bladeId)
if err != nil {
return formatErrorResp(ctx, err.(*common.RequestError))
}

// Make sure the newBladeId doesn't exist
existBladeIds := appliance.GetAllBladeIds()
for _, id := range existBladeIds {
if newBladeId == id {
err := common.RequestError{
StatusCode: common.StatusBladeIdDuplicate,
Err: fmt.Errorf("the new name (%s) already exists", newBladeId),
}
return formatErrorResp(ctx, &err)
}
}

//Rename the appliance with the new id
newBlade, err := manager.RenameBlade(ctx, appliance, blade, newBladeId)
if err != nil {
return formatErrorResp(ctx, err.(*common.RequestError))
}

return openapi.Response(http.StatusOK, newBlade), nil
}

// BladesGetResourceById -
func (cfm *CfmApiService) BladesGetResourceById(ctx context.Context, applianceId string, bladeId string, resourceId string) (openapi.ImplResponse, error) {
appliance, err := manager.GetApplianceById(ctx, applianceId)
Expand Down Expand Up @@ -932,6 +994,34 @@ func (cfm *CfmApiService) HostsPost(ctx context.Context, credentials openapi.Cre
return openapi.Response(http.StatusCreated, h), nil
}

// HostsUpdateById -
func (cfm *CfmApiService) HostsUpdateById(ctx context.Context, hostId string, newHostId string) (openapi.ImplResponse, error) {
// Make sure the hostId exists
// Get the host information from the manager level and is used for renaming
host, err := manager.GetHostById(ctx, hostId)
if err != nil {
return formatErrorResp(ctx, err.(*common.RequestError))
}

// Make sure the newHostId doesn't exist
_, exist := manager.GetHostById(ctx, newHostId)
if exist == nil {
err := common.RequestError{
StatusCode: common.StatusHostIdDuplicate,
Err: fmt.Errorf("the new name (%s) already exists", newHostId),
}
return formatErrorResp(ctx, &err)
}

//Rename the cxl host with the new id
newHost, err := manager.RenameHost(ctx, host, newHostId)
if err != nil {
return formatErrorResp(ctx, err.(*common.RequestError))
}

return openapi.Response(http.StatusOK, newHost), nil
}

// HostsResync -
func (cfm *CfmApiService) HostsResyncById(ctx context.Context, hostId string) (openapi.ImplResponse, error) {
host, err := manager.ResyncHostById(ctx, hostId)
Expand Down
15 changes: 14 additions & 1 deletion pkg/common/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ const (
StatusBladeResyncFailure //409
StatusHostResyncFailure //409

StatusApplianceRenameFailure //409
StatusBladeRenameFailure //409
StatusHostRenameFailure //409

StatusApplianceIdDuplicate //409
StatusBladeIdDuplicate //409
StatusMemoryIdDuplicate //409
Expand Down Expand Up @@ -176,6 +180,12 @@ func (e StatusCodeType) String() string {
return "Port Id Already Exist"
case StatusHostIdDuplicate:
return "Host Id Already Exist"
case StatusApplianceRenameFailure:
return "Rename Appliance Failure"
case StatusBladeRenameFailure:
return "Rename Blade Failure"
case StatusHostRenameFailure:
return "Rename Host Failure"
}
return "Unknown"

Expand Down Expand Up @@ -220,7 +230,10 @@ func (e StatusCodeType) HttpStatusCode() int {
StatusApplianceIdDuplicate,
StatusBladeIdDuplicate,
StatusPortIdDuplicate,
StatusHostIdDuplicate:
StatusHostIdDuplicate,
StatusApplianceRenameFailure,
StatusBladeRenameFailure,
StatusHostRenameFailure:
return http.StatusConflict // 409
case StatusBackendInterfaceFailure,
StatusBladeCreateSessionFailure,
Expand Down
21 changes: 20 additions & 1 deletion pkg/manager/appliance.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,25 @@ func (a *Appliance) InvalidateCache() {
}
}

func (a *Appliance) AddBladeBack(ctx context.Context, c *openapi.Credentials) (*Blade, error) {
logger := klog.FromContext(ctx)
logger.V(4).Info(">>>>>> Add Blade Back: ", "bladeId", c.CustomId, "applianceId", a.Id)

// add blade back
blade, err := a.AddBlade(ctx, c)
if err != nil {
newErr := fmt.Errorf("failed to add blade [%s] back", c.CustomId)
logger.Error(newErr, "failure: add blade back")
return nil, &common.RequestError{StatusCode: common.StatusBladeCreateSessionFailure, Err: newErr}
}

blade.UpdateConnectionStatusBackend(ctx)

logger.V(2).Info("success: add blade back", "status", blade.Status, "bladeId", blade.Id, "applianceId", a.Id)

return blade, nil
}

func (a *Appliance) ResyncBladeById(ctx context.Context, bladeId string) (*Blade, error) {
logger := klog.FromContext(ctx)
logger.V(4).Info(">>>>>> ResyncBladeById: ", "bladeId", bladeId, "applianceId", a.Id)
Expand All @@ -305,7 +324,7 @@ func (a *Appliance) ResyncBladeById(ctx context.Context, bladeId string) (*Blade
if !ok || blade == nil {
newErr := fmt.Errorf("failed to get blade [%s]", bladeId)
logger.Error(newErr, "failure: resync blade by id")
return nil, &common.RequestError{StatusCode: common.StatusHostIdDoesNotExist, Err: newErr}
return nil, &common.RequestError{StatusCode: common.StatusBladeIdDoesNotExist, Err: newErr}
}

blade.UpdateConnectionStatusBackend(ctx)
Expand Down
Loading

0 comments on commit 06dbdf1

Please sign in to comment.