Skip to content

Commit

Permalink
add ext plugin, replace _ into - in plugin names
Browse files Browse the repository at this point in the history
  • Loading branch information
suslovsergey committed Feb 9, 2022
1 parent 6044a4f commit 9e25bf4
Show file tree
Hide file tree
Showing 3 changed files with 208 additions and 32 deletions.
87 changes: 87 additions & 0 deletions apisix/model/plugin_ext_plugin_post_req.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package model

import (
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/webbankir/terraform-provider-apisix/apisix/plan_modifier"
"github.com/webbankir/terraform-provider-apisix/apisix/utils"
)

type PluginExtPluginPostReqType struct {
Disable types.Bool `tfsdk:"disable"`
Config []PluginExtPluginPostReqConfType `tfsdk:"conf"`
}

type PluginExtPluginPostReqConfType struct {
Name types.String `tfsdk:"name"`
Value types.String `tfsdk:"value"`
}

var PluginExtPluginPostReqSchemaAttribute = tfsdk.Attribute{
Optional: true,
Attributes: tfsdk.SingleNestedAttributes(map[string]tfsdk.Attribute{
"disable": {
Optional: true,
Computed: true,
Type: types.BoolType,
PlanModifiers: []tfsdk.AttributePlanModifier{
plan_modifier.DefaultBool(false),
},
},
"conf": {
Required: true,
Attributes: tfsdk.ListNestedAttributes(map[string]tfsdk.Attribute{
"name": {
Required: true,
Type: types.StringType,
},
"value": {
Required: true,
Type: types.StringType,
},
}, tfsdk.ListNestedAttributesOptions{MinItems: 1}),
},
}),
}

func (s PluginExtPluginPostReqType) Name() string { return "ext-plugin-pre-req" }

func (s PluginExtPluginPostReqType) MapToState(data map[string]interface{}, pluginsType *PluginsType) {
v := data[s.Name()]
if v == nil {
return
}
jsonData := v.(map[string]interface{})
item := PluginExtPluginPostReqType{}

utils.MapValueToBoolTypeValue(jsonData, "disable", &item.Disable)

var subItems []PluginExtPluginPostReqConfType
for _, vv := range jsonData["conf"].([]interface{}) {
subItem := PluginExtPluginPostReqConfType{}
subV := vv.(map[string]interface{})
utils.MapValueToStringTypeValue(subV, "name", &subItem.Name)
utils.MapValueToStringTypeValue(subV, "value", &subItem.Value)
subItems = append(subItems, subItem)
}

item.Config = subItems
pluginsType.ExtPluginPostReqType = &item
}

func (s PluginExtPluginPostReqType) StateToMap(m map[string]interface{}) {
pluginValue := map[string]interface{}{}

utils.BoolTypeValueToMap(s.Disable, pluginValue, "disable")

var subItems []map[string]interface{}
for _, vv := range s.Config {
subItem := make(map[string]interface{})
utils.StringTypeValueToMap(vv.Name, subItem, "name")
utils.StringTypeValueToMap(vv.Value, subItem, "value")
}

pluginValue["config"] = subItems

m[s.Name()] = pluginValue
}
87 changes: 87 additions & 0 deletions apisix/model/plugin_ext_plugin_pre_req.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
package model

import (
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/webbankir/terraform-provider-apisix/apisix/plan_modifier"
"github.com/webbankir/terraform-provider-apisix/apisix/utils"
)

type PluginExtPluginPreReqType struct {
Disable types.Bool `tfsdk:"disable"`
Config []PluginExtPluginPreReqConfType `tfsdk:"conf"`
}

type PluginExtPluginPreReqConfType struct {
Name types.String `tfsdk:"name"`
Value types.String `tfsdk:"value"`
}

var PluginExtPluginPreReqSchemaAttribute = tfsdk.Attribute{
Optional: true,
Attributes: tfsdk.SingleNestedAttributes(map[string]tfsdk.Attribute{
"disable": {
Optional: true,
Computed: true,
Type: types.BoolType,
PlanModifiers: []tfsdk.AttributePlanModifier{
plan_modifier.DefaultBool(false),
},
},
"conf": {
Required: true,
Attributes: tfsdk.ListNestedAttributes(map[string]tfsdk.Attribute{
"name": {
Required: true,
Type: types.StringType,
},
"value": {
Required: true,
Type: types.StringType,
},
}, tfsdk.ListNestedAttributesOptions{MinItems: 1}),
},
}),
}

func (s PluginExtPluginPreReqType) Name() string { return "ext-plugin-pre-req" }

func (s PluginExtPluginPreReqType) MapToState(data map[string]interface{}, pluginsType *PluginsType) {
v := data[s.Name()]
if v == nil {
return
}
jsonData := v.(map[string]interface{})
item := PluginExtPluginPreReqType{}

utils.MapValueToBoolTypeValue(jsonData, "disable", &item.Disable)

var subItems []PluginExtPluginPreReqConfType
for _, vv := range jsonData["conf"].([]interface{}) {
subItem := PluginExtPluginPreReqConfType{}
subV := vv.(map[string]interface{})
utils.MapValueToStringTypeValue(subV, "name", &subItem.Name)
utils.MapValueToStringTypeValue(subV, "value", &subItem.Value)
subItems = append(subItems, subItem)
}

item.Config = subItems
pluginsType.ExtPluginPreReqType = &item
}

func (s PluginExtPluginPreReqType) StateToMap(m map[string]interface{}) {
pluginValue := map[string]interface{}{}

utils.BoolTypeValueToMap(s.Disable, pluginValue, "disable")

var subItems []map[string]interface{}
for _, vv := range s.Config {
subItem := make(map[string]interface{})
utils.StringTypeValueToMap(vv.Name, subItem, "name")
utils.StringTypeValueToMap(vv.Value, subItem, "value")
}

pluginValue["config"] = subItems

m[s.Name()] = pluginValue
}
66 changes: 34 additions & 32 deletions apisix/model/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import (
//batch-requests
//echo
//server-info
//ext-plugin-pre-req
//ext-plugin-post-req
//grpc-transcode
//fault-injection
//key-auth
Expand Down Expand Up @@ -56,48 +54,52 @@ type PluginCommonInterface interface {

type PluginsType struct {
//Custom *[]PluginCustomType `tfsdk:"custom"`
BasicAuth *PluginBasicAuthType `tfsdk:"basic_auth"`
ConsumerRestriction *PluginConsumerRestrictionType `tfsdk:"consumer_restriction"`
BasicAuth *PluginBasicAuthType `tfsdk:"basic-auth"`
ConsumerRestriction *PluginConsumerRestrictionType `tfsdk:"consumer-restriction"`
Cors *PluginCorsType `tfsdk:"cors"`
ExtPluginPostReqType *PluginExtPluginPreReqType `tfsdk:"ext-plugin-post-req"`
ExtPluginPreReqType *PluginExtPluginPreReqType `tfsdk:"ext-plugin-pre-req"`
GELFUDPLogger *PluginGELFUDPLoggerType `tfsdk:"gelf-udp-logger"`
GZIP *PluginGZIPType `tfsdk:"gzip"`
HTTPLogger *PluginHTTPLoggerType `tfsdk:"http_logger"`
GELFUDPLogger *PluginGELFUDPLoggerType `tfsdk:"gelf_udp_logger"`
IpRestriction *PluginIpRestrictionType `tfsdk:"ip_restriction"`
HTTPLogger *PluginHTTPLoggerType `tfsdk:"http-logger"`
Headers *PluginHeadersType `tfsdk:"headers"`
IpRestriction *PluginIpRestrictionType `tfsdk:"ip-restriction"`
MultiResponseRewrite *PluginMultiResponseRewriteType `tfsdk:"multi-response-rewrite"`
Prometheus *PluginPrometheusType `tfsdk:"prometheus"`
ProxyCache *PluginProxyCacheType `tfsdk:"proxy_cache"`
ProxyRewrite *PluginProxyRewriteType `tfsdk:"proxy_rewrite"`
RealIP *PluginRealIPType `tfsdk:"real_ip"`
ProxyCache *PluginProxyCacheType `tfsdk:"proxy-cache"`
ProxyRewrite *PluginProxyRewriteType `tfsdk:"proxy-rewrite"`
RealIP *PluginRealIPType `tfsdk:"real-ip"`
Redirect *PluginRedirectType `tfsdk:"redirect"`
RedirectRegex *PluginRedirectRegexType `tfsdk:"redirect_regex"`
RequestId *PluginRequestIdType `tfsdk:"request_id"`
ResponseRewrite *PluginResponseRewriteType `tfsdk:"response_rewrite"`
ServerlessPostFunction *PluginServerlessPostFunctionType `tfsdk:"serverless_post_function"`
ServerlessPreFunction *PluginServerlessPreFunctionType `tfsdk:"serverless_pre_function"`
Headers *PluginHeadersType `tfsdk:"headers"`
MultiResponseRewrite *PluginMultiResponseRewriteType `tfsdk:"multi_response_rewrite"`
RedirectRegex *PluginRedirectRegexType `tfsdk:"redirect-regex"`
RequestId *PluginRequestIdType `tfsdk:"request-id"`
ResponseRewrite *PluginResponseRewriteType `tfsdk:"response-rewrite"`
ServerlessPostFunction *PluginServerlessPostFunctionType `tfsdk:"serverless-post-function"`
ServerlessPreFunction *PluginServerlessPreFunctionType `tfsdk:"serverless-pre-function"`
Syslog *PluginSyslogType `tfsdk:"syslog"`
}

var PluginsSchemaAttribute = tfsdk.SingleNestedAttributes(map[string]tfsdk.Attribute{
"basic_auth": PluginBasicAuthSchemaAttribute,
"consumer_restriction": PluginConsumerRestrictionSchemaAttribute,
"basic-auth": PluginBasicAuthSchemaAttribute,
"consumer-restriction": PluginConsumerRestrictionSchemaAttribute,
"cors": PluginCorsSchemaAttribute,
"ext-plugin-post-req": PluginExtPluginPostReqSchemaAttribute,
"ext-plugin-pre-req": PluginExtPluginPreReqSchemaAttribute,
"gelf-udp-logger": PluginGELFUDPLoggerSchemaAttribute,
"gzip": PluginGZIPSchemaAttribute,
"http_logger": PluginHTTPLoggerSchemaAttribute,
"ip_restriction": PluginIpRestrictionSchemaAttribute,
"headers": PluginHeadersSchemaAttribute,
"http-logger": PluginHTTPLoggerSchemaAttribute,
"ip-restriction": PluginIpRestrictionSchemaAttribute,
"multi-response-rewrite": PluginMultiResponseRewriteSchemaAttribute,
"prometheus": PluginPrometheusSchemaAttribute,
"proxy_cache": PluginProxyCacheSchemaAttribute,
"proxy_rewrite": PluginProxyRewriteSchemaAttribute,
"real_ip": PluginRealIPSchemaAttribute,
"proxy-cache": PluginProxyCacheSchemaAttribute,
"proxy-rewrite": PluginProxyRewriteSchemaAttribute,
"real-ip": PluginRealIPSchemaAttribute,
"redirect": PluginRedirectSchemaAttribute,
"redirect_regex": PluginRedirectRegexSchemaAttribute,
"request_id": PluginRequestIdSchemaAttribute,
"response_rewrite": PluginResponseRewriteSchemaAttribute,
"serverless_post_function": PluginServerlessPostFunctionSchemaAttribute,
"serverless_pre_function": PluginServerlessPreFunctionSchemaAttribute,
"headers": PluginHeadersSchemaAttribute,
"multi_response_rewrite": PluginMultiResponseRewriteSchemaAttribute,
"redirect-regex": PluginRedirectRegexSchemaAttribute,
"request-id": PluginRequestIdSchemaAttribute,
"response-rewrite": PluginResponseRewriteSchemaAttribute,
"serverless-post-function": PluginServerlessPostFunctionSchemaAttribute,
"serverless-pre-function": PluginServerlessPreFunctionSchemaAttribute,
"syslog": PluginSyslogSchemaAttribute,
"gelf_udp_logger": PluginGELFUDPLoggerSchemaAttribute,
//"custom": PluginCustomSchemaAttribute,
})

0 comments on commit 9e25bf4

Please sign in to comment.