2022-01-23 19:48:04 +00:00
package fdo
import (
"errors"
"fmt"
"net/http"
"strconv"
httperror "github.com/portainer/libhttp/error"
"github.com/portainer/libhttp/request"
"github.com/portainer/libhttp/response"
portainer "github.com/portainer/portainer/api"
)
// @id updateProfile
// @summary updates an existing FDO Profile
// @description updates an existing FDO Profile
// @description **Access policy**: administrator
// @tags intel
// @security jwt
// @produce json
// @success 200 "Success"
// @failure 400 "Invalid request"
// @failure 409 "Profile name already exists"
// @failure 500 "Server error"
// @router /fdo/profiles/{id} [put]
func ( handler * Handler ) updateProfile ( w http . ResponseWriter , r * http . Request ) * httperror . HandlerError {
id , err := request . RetrieveNumericRouteVariableValue ( r , "id" )
if err != nil {
2022-09-14 23:42:39 +00:00
return httperror . BadRequest ( "Bad request" , errors . New ( "missing 'id' query parameter" ) )
2022-01-23 19:48:04 +00:00
}
var payload createProfileFromFileContentPayload
err = request . DecodeAndValidateJSONPayload ( r , & payload )
if err != nil {
2022-09-14 23:42:39 +00:00
return httperror . BadRequest ( "Invalid request payload" , err )
2022-01-23 19:48:04 +00:00
}
profile , err := handler . DataStore . FDOProfile ( ) . FDOProfile ( portainer . FDOProfileID ( id ) )
if handler . DataStore . IsErrObjectNotFound ( err ) {
2022-09-14 23:42:39 +00:00
return httperror . NotFound ( "Unable to find a FDO Profile with the specified identifier inside the database" , err )
2022-01-23 19:48:04 +00:00
} else if err != nil {
2022-09-14 23:42:39 +00:00
return httperror . InternalServerError ( "Unable to find a FDO Profile with the specified identifier inside the database" , err )
2022-01-23 19:48:04 +00:00
}
isUnique , err := handler . checkUniqueProfileName ( payload . Name , id )
if err != nil {
2022-09-14 23:42:39 +00:00
return httperror . InternalServerError ( err . Error ( ) , err )
2022-01-23 19:48:04 +00:00
}
if ! isUnique {
2022-09-14 23:42:39 +00:00
return & httperror . HandlerError { StatusCode : http . StatusConflict , Message : fmt . Sprintf ( "A profile with the name '%s' already exists" , payload . Name ) , Err : errors . New ( "a profile already exists with this name" ) }
2022-01-23 19:48:04 +00:00
}
filePath , err := handler . FileService . StoreFDOProfileFileFromBytes ( strconv . Itoa ( int ( profile . ID ) ) , [ ] byte ( payload . ProfileFileContent ) )
if err != nil {
2022-09-14 23:42:39 +00:00
return httperror . InternalServerError ( "Unable to update profile" , err )
2022-01-23 19:48:04 +00:00
}
profile . FilePath = filePath
profile . Name = payload . Name
err = handler . DataStore . FDOProfile ( ) . Update ( profile . ID , profile )
if err != nil {
2022-09-14 23:42:39 +00:00
return httperror . InternalServerError ( "Unable to update profile" , err )
2022-01-23 19:48:04 +00:00
}
return response . JSON ( w , profile )
}