Skip to content

Commit

Permalink
ADD action runtime type "condition".
Browse files Browse the repository at this point in the history
ADD action runtime type "condition".
  • Loading branch information
karminski committed Jan 23, 2024
1 parent 22a4051 commit 12d553a
Show file tree
Hide file tree
Showing 2 changed files with 121 additions and 0 deletions.
60 changes: 60 additions & 0 deletions src/actionruntime/condition/services.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
// Copyright 2022 The ILLA Authors.
//
// 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.

package condition

import (
"errors"
"fmt"

"github.com/illacloud/builder-backend/src/actionruntime/common"
)

type ConditionConnector struct {
Action ConditionTemplate
}

// server side transformer have no validate resource options method
func (r *ConditionConnector) ValidateResourceOptions(resourceOptions map[string]interface{}) (common.ValidateResult, error) {
return common.ValidateResult{Valid: true}, nil
}

func (r *ConditionConnector) ValidateActionTemplate(actionOptions map[string]interface{}) (common.ValidateResult, error) {
fmt.Printf("[DUMP] actionOptions: %+v \n", actionOptions)
// @todo: check action needed field
return common.ValidateResult{Valid: true}, nil
}

// server side transformer have no test connection method
func (r *ConditionConnector) TestConnection(resourceOptions map[string]interface{}) (common.ConnectionResult, error) {
return common.ConnectionResult{Success: false}, errors.New("unsupported type: server side transformer")
}

// server side transformer have no meta info
func (r *ConditionConnector) GetMetaInfo(resourceOptions map[string]interface{}) (common.MetaInfoResult, error) {
return common.MetaInfoResult{Success: false}, errors.New("unsupported type: server side transformer")
}

func (r *ConditionConnector) Run(resourceOptions map[string]interface{}, actionOptions map[string]interface{}, rawActionOptions map[string]interface{}) (common.RuntimeResult, error) {
res := common.RuntimeResult{
Success: false,
Rows: []map[string]interface{}{},
Extra: map[string]interface{}{},
}

fmt.Printf("[DUMP] ConditionConnector.Run() actionOptions: %+v\n", actionOptions)

fmt.Printf("[DUMP] res: %+v\n", res)
return res, nil
}
61 changes: 61 additions & 0 deletions src/actionruntime/condition/types.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// Copyright 2022 The ILLA Authors.
//
// 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.

package condition

import "errors"

type ConditionTemplate struct {
URL string
Method string `validate:"oneof=GET POST PUT PATCH DELETE HEAD OPTIONS"`
BodyType string `validate:"oneof=none form-data x-www-form-urlencoded raw json binary"`
UrlParams []map[string]string
Headers []map[string]string
Body interface{} `validate:"required_unless=BodyType none"`
Cookies []map[string]string
}

type RawBody struct {
Type string `json:"type"`
Content string `json:"content"`
}

func (t *ConditionTemplate) ReflectBodyToRaw() *RawBody {
rbd := &RawBody{}
rb, _ := t.Body.(map[string]interface{})
for k, v := range rb {
switch k {
case "type":
rbd.Type, _ = v.(string)
case "content":
rbd.Content, _ = v.(string)
}
}
return rbd
}

func resolveIntFieldsFromActionOptions(actionOptions map[string]interface{}, fieldName string) (int, error) {
raw, hit := actionOptions[fieldName]
if !hit {
return 0, errors.New("missing " + fieldName + " field")

}
numberInFloat, numberAssertPass := raw.(float64)
number := int(numberInFloat)
if !numberAssertPass {
return 0, errors.New(fieldName + " field which in action options assert failed")

}
return number, nil
}

0 comments on commit 12d553a

Please sign in to comment.