2019-05-03 10:47:22 +00:00
// Copyright 2019 Sorint.lab
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied
// See the License for the specific language governing permissions and
// limitations under the License.
2019-05-03 21:35:25 +00:00
package action
2019-05-03 10:47:22 +00:00
import (
"context"
"encoding/json"
2019-07-01 09:40:20 +00:00
"agola.io/agola/internal/datamanager"
"agola.io/agola/internal/db"
"agola.io/agola/internal/services/types"
"agola.io/agola/internal/util"
2019-05-03 10:47:22 +00:00
uuid "github.com/satori/go.uuid"
2019-05-23 09:23:14 +00:00
errors "golang.org/x/xerrors"
2019-05-03 10:47:22 +00:00
)
2019-05-12 22:23:08 +00:00
func ( h * ActionHandler ) GetVariables ( ctx context . Context , parentType types . ConfigType , parentRef string , tree bool ) ( [ ] * types . Variable , error ) {
var variables [ ] * types . Variable
err := h . readDB . Do ( func ( tx * db . Tx ) error {
parentID , err := h . readDB . ResolveConfigID ( tx , parentType , parentRef )
if err != nil {
return err
}
if tree {
variables , err = h . readDB . GetVariablesTree ( tx , parentType , parentID )
} else {
variables , err = h . readDB . GetVariables ( tx , parentID )
}
return err
} )
if err != nil {
return nil , err
}
return variables , nil
}
2019-05-03 21:35:25 +00:00
func ( h * ActionHandler ) CreateVariable ( ctx context . Context , variable * types . Variable ) ( * types . Variable , error ) {
2019-05-03 10:47:22 +00:00
if variable . Name == "" {
return nil , util . NewErrBadRequest ( errors . Errorf ( "variable name required" ) )
}
if ! util . ValidateName ( variable . Name ) {
return nil , util . NewErrBadRequest ( errors . Errorf ( "invalid variable name %q" , variable . Name ) )
}
if len ( variable . Values ) == 0 {
return nil , util . NewErrBadRequest ( errors . Errorf ( "variable values required" ) )
}
if variable . Parent . Type == "" {
return nil , util . NewErrBadRequest ( errors . Errorf ( "variable parent type required" ) )
}
if variable . Parent . ID == "" {
return nil , util . NewErrBadRequest ( errors . Errorf ( "variable parent id required" ) )
}
if variable . Parent . Type != types . ConfigTypeProject && variable . Parent . Type != types . ConfigTypeProjectGroup {
return nil , util . NewErrBadRequest ( errors . Errorf ( "invalid variable parent type %q" , variable . Parent . Type ) )
}
var cgt * datamanager . ChangeGroupsUpdateToken
// changegroup is the variable name
cgNames := [ ] string { util . EncodeSha256Hex ( "variablename-" + variable . Name ) }
// must do all the checks in a single transaction to avoid concurrent changes
2019-05-03 21:35:25 +00:00
err := h . readDB . Do ( func ( tx * db . Tx ) error {
2019-05-03 10:47:22 +00:00
var err error
2019-05-03 21:35:25 +00:00
cgt , err = h . readDB . GetChangeGroupsUpdateTokens ( tx , cgNames )
2019-05-03 10:47:22 +00:00
if err != nil {
return err
}
2019-05-03 21:35:25 +00:00
parentID , err := h . readDB . ResolveConfigID ( tx , variable . Parent . Type , variable . Parent . ID )
2019-05-03 10:47:22 +00:00
if err != nil {
return err
}
variable . Parent . ID = parentID
// check duplicate variable name
2019-05-03 21:35:25 +00:00
s , err := h . readDB . GetVariableByName ( tx , variable . Parent . ID , variable . Name )
2019-05-03 10:47:22 +00:00
if err != nil {
return err
}
if s != nil {
return util . NewErrBadRequest ( errors . Errorf ( "variable with name %q for %s with id %q already exists" , variable . Name , variable . Parent . Type , variable . Parent . ID ) )
}
return nil
} )
if err != nil {
return nil , err
}
variable . ID = uuid . NewV4 ( ) . String ( )
variablej , err := json . Marshal ( variable )
if err != nil {
2019-05-23 09:23:14 +00:00
return nil , errors . Errorf ( "failed to marshal variable: %w" , err )
2019-05-03 10:47:22 +00:00
}
actions := [ ] * datamanager . Action {
{
ActionType : datamanager . ActionTypePut ,
DataType : string ( types . ConfigTypeVariable ) ,
ID : variable . ID ,
Data : variablej ,
} ,
}
2019-05-03 21:35:25 +00:00
_ , err = h . dm . WriteWal ( ctx , actions , cgt )
2019-05-03 10:47:22 +00:00
return variable , err
}
2019-05-03 21:35:25 +00:00
func ( h * ActionHandler ) DeleteVariable ( ctx context . Context , parentType types . ConfigType , parentRef , variableName string ) error {
2019-05-03 10:47:22 +00:00
var variable * types . Variable
var cgt * datamanager . ChangeGroupsUpdateToken
// must do all the checks in a single transaction to avoid concurrent changes
2019-05-03 21:35:25 +00:00
err := h . readDB . Do ( func ( tx * db . Tx ) error {
2019-05-03 10:47:22 +00:00
var err error
2019-05-03 21:35:25 +00:00
parentID , err := h . readDB . ResolveConfigID ( tx , parentType , parentRef )
2019-05-03 10:47:22 +00:00
if err != nil {
return err
}
// check variable existance
2019-05-03 21:35:25 +00:00
variable , err = h . readDB . GetVariableByName ( tx , parentID , variableName )
2019-05-03 10:47:22 +00:00
if err != nil {
return err
}
if variable == nil {
return util . NewErrBadRequest ( errors . Errorf ( "variable with name %q doesn't exist" , variableName ) )
}
// changegroup is the variable id
cgNames := [ ] string { util . EncodeSha256Hex ( "variableid-" + variable . ID ) }
2019-05-03 21:35:25 +00:00
cgt , err = h . readDB . GetChangeGroupsUpdateTokens ( tx , cgNames )
2019-05-03 10:47:22 +00:00
if err != nil {
return err
}
return nil
} )
if err != nil {
return err
}
actions := [ ] * datamanager . Action {
{
ActionType : datamanager . ActionTypeDelete ,
DataType : string ( types . ConfigTypeVariable ) ,
ID : variable . ID ,
} ,
}
2019-05-03 21:35:25 +00:00
_ , err = h . dm . WriteWal ( ctx , actions , cgt )
2019-05-03 10:47:22 +00:00
return err
}