Skip to content

Commit

Permalink
feat: add api bladesRenameById
Browse files Browse the repository at this point in the history
  • Loading branch information
Meng-20 committed Oct 2, 2024
1 parent 24e7f27 commit 013976a
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 1 deletion.
39 changes: 39 additions & 0 deletions api/cfm-openapi.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,45 @@ paths:
schema:
$ref: "#/components/schemas/statusMessage"

# Rename Blade
/cfm/v1/appliances/{applianceId}/blades/{bladeId}/rename:
post:
description: Rename a memory blade by id.
operationId: bladesRenameById
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:
get:
Expand Down
35 changes: 35 additions & 0 deletions pkg/api/api_default_service.go
Original file line number Diff line number Diff line change
Expand Up @@ -522,6 +522,41 @@ func (cfm *CfmApiService) BladesGetPorts(ctx context.Context, applianceId string
return openapi.Response(http.StatusOK, response), nil
}

// BladesRenameById -
func (s *CfmApiService) BladesRenameById(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, 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
42 changes: 41 additions & 1 deletion pkg/manager/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func RenameAppliance(ctx context.Context, appliance *Appliance, newApplianceId s
existingAppliance, ok := deviceCache.GetApplianceByIdOk(appliance.Id)
if !ok {
newErr := fmt.Errorf("failed to get appliance [%s]", appliance.Id)
logger.Error(newErr, "failure: delete appliance by id")
logger.Error(newErr, "failure: get appliance by id")
return nil, &common.RequestError{StatusCode: common.StatusApplianceIdDoesNotExist, Err: newErr}
}

Expand Down Expand Up @@ -182,6 +182,46 @@ func RenameAppliance(ctx context.Context, appliance *Appliance, newApplianceId s
}
}

func RenameBlade(ctx context.Context, blade *Blade, newBladeId string) (*Blade, error) {
logger := klog.FromContext(ctx)
logger.V(4).Info(">>>>>> RenameBladeById: ", "bladeId", blade.Id)

// query cache
appliance, ok := deviceCache.GetApplianceByIdOk(blade.ApplianceId)
if !ok {
newErr := fmt.Errorf("failed to get appliance [%s]", blade.ApplianceId)
logger.Error(newErr, "failure: get appliance by id")
return nil, &common.RequestError{StatusCode: common.StatusApplianceIdDoesNotExist, Err: newErr}
}

// Save the blade credentials for adding back with the new name
c := &openapi.Credentials{
Username: blade.creds.Username,
Password: blade.creds.Password,
IpAddress: blade.creds.IpAddress,
Port: blade.creds.Port,
Insecure: blade.creds.Insecure,
Protocol: blade.creds.Protocol,
CustomId: newBladeId,
}

// delete blade
_, err := appliance.DeleteBladeById(ctx, blade.Id)
if err != nil {
newErr := fmt.Errorf("failed to delete blade [%s]: %w", blade.Id, err)
logger.Error(newErr, "failure: delete blade by id")
return nil, &common.RequestError{StatusCode: common.StatusBladeDeleteSessionFailure, Err: newErr}
}
// Add the balde back with the new name
newBlade, err := appliance.AddBlade(ctx, c)
if err != nil {
newErr := fmt.Errorf("failed to add blade [%s]: %w", newBladeId, err)
logger.Error(newErr, "failure: add blade with new id")
return nil, &common.RequestError{StatusCode: common.StatusBladeCreateSessionFailure, Err: newErr}
}
return newBlade, nil
}

func ResyncApplianceById(ctx context.Context, applianceId string) (*Appliance, error) {
logger := klog.FromContext(ctx)
logger.V(4).Info(">>>>>> ResyncApplianceById: ", "applianceId", applianceId)
Expand Down

0 comments on commit 013976a

Please sign in to comment.