diff --git a/api/cfm-openapi.yaml b/api/cfm-openapi.yaml index 3d1848b..b15c7a9 100644 --- a/api/cfm-openapi.yaml +++ b/api/cfm-openapi.yaml @@ -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: diff --git a/pkg/api/api_default_service.go b/pkg/api/api_default_service.go index 2c8bbe0..51cdd8e 100644 --- a/pkg/api/api_default_service.go +++ b/pkg/api/api_default_service.go @@ -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) diff --git a/pkg/manager/manager.go b/pkg/manager/manager.go index b93e43b..bd12239 100644 --- a/pkg/manager/manager.go +++ b/pkg/manager/manager.go @@ -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} } @@ -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)