-
Notifications
You must be signed in to change notification settings - Fork 51
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ADD action runtime type "condition".
ADD action runtime type "condition".
- Loading branch information
Showing
2 changed files
with
121 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |