Permalink
Show file tree
Hide file tree
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
feat: add mem object group & apisix model (#14)
* feat: add mem object group & apisix model * merge go.mod
- Loading branch information
Showing
10 changed files
with
461 additions
and
2 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
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
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,96 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 adapter | ||
|
||
import ( | ||
"github.com/apache/apisix-control-plane/pkg/dp/apisix" | ||
"github.com/apache/apisix-control-plane/pkg/mem" | ||
"reflect" | ||
) | ||
|
||
func ToRoute(r *mem.Route) *apisix.Route { | ||
return &apisix.Route{ | ||
Name: r.FullName, | ||
Hosts: r.Hosts, | ||
Desc: r.Name, | ||
Uris: toUris(r), | ||
Vars: toVars(r), | ||
Methods: r.Methods, | ||
} | ||
} | ||
|
||
func toUris(r *mem.Route) []*string { | ||
result := make([]*string, 0) | ||
for _, m := range r.Match { | ||
// uris | ||
matchUris := m["uris"] | ||
switch reflect.TypeOf(matchUris).Kind() { | ||
case reflect.Slice: | ||
s := reflect.ValueOf(matchUris) | ||
if slice, ok := s.Interface().([]map[string]string); ok { | ||
for _, s := range slice { | ||
for k, v := range s { | ||
switch k { | ||
case "prefix": | ||
uri := v + "*" | ||
result = append(result, &uri) | ||
case "exact": | ||
result = append(result, &v) | ||
} | ||
} | ||
} | ||
} | ||
} | ||
} | ||
return result | ||
} | ||
|
||
func toVars(r *mem.Route) [][]*string { | ||
result := make([][]*string, 0) | ||
for _, m := range r.Match { | ||
// args | ||
matchArgs := m["args"] | ||
switch reflect.TypeOf(matchArgs).Kind() { | ||
case reflect.Slice: | ||
s := reflect.ValueOf(matchArgs) | ||
if slice, ok := s.Interface().([]map[string]map[string]string); ok { | ||
for _, s := range slice { | ||
tmp := make([]*string, 0) | ||
for p, value := range s { | ||
tmp = append(tmp, &p) | ||
for k, v := range value { | ||
switch k { | ||
case "greater": | ||
greater := ">" | ||
tmp = append(tmp, &greater) | ||
tmp = append(tmp, &v) | ||
case "exact": | ||
equals := "==" | ||
tmp = append(tmp, &equals) | ||
tmp = append(tmp, &v) | ||
} | ||
} | ||
result = append(result, tmp) | ||
} | ||
|
||
} | ||
} | ||
} | ||
} | ||
return result | ||
} |
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,23 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 conf | ||
|
||
var ( | ||
ApiKey = "edd1c9f034335f136f87ad84b625c8f1" | ||
BaseUrl = "http://127.0.0.1:9080/apisix/admin" | ||
) |
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,53 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 apisix | ||
|
||
type Route struct { | ||
ID *string `json:"id,omitempty"` | ||
Hosts []*string `json:"hosts,omitempty"` | ||
Name *string `json:"name,omitempty"` | ||
Desc *string `json:"desc,omitempty"` | ||
Uris []*string `json:"uris"` | ||
Vars [][]*string `json:"vars,omitempty"` | ||
Methods []*string `json:"methods,omitempty"` | ||
ServiceId *string `json:"service_id,omitempty"` | ||
ServiceName *string `json:"service_name,omitempty"` | ||
UpstreamId *string `json:"upstream_id,omitempty"` | ||
UpstreamName *string `json:"upstream_name,omitempty"` | ||
Plugins []*Plugin `json:"plugins,omitempty"` | ||
} | ||
|
||
type Plugin map[string]interface{} | ||
|
||
type Upstream struct { | ||
ID *string `json:"id,omitempty"` | ||
Group *string `json:"group,omitempty"` | ||
ResourceVersion *string `json:"resource_version,omitempty"` | ||
Name *string `json:"name,omitempty"` | ||
Type *string `json:"type,omitempty"` | ||
HashOn *string `json:"hash_on,omitemtpy"` | ||
Key *string `json:"key,omitempty"` | ||
Nodes []*Node `json:"nodes,omitempty"` | ||
FromKind *string `json:"from_kind,omitempty"` | ||
} | ||
|
||
type Node struct { | ||
IP *string `json:"ip,omitempty"` | ||
Port *int `json:"port,omitempty"` | ||
Weight *int `json:"weight,omitempty"` | ||
} |
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,79 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 apisix | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/apache/apisix-control-plane/pkg/conf" | ||
"github.com/apache/apisix-control-plane/pkg/utils" | ||
) | ||
|
||
func (route *Route) Add() (*RouteResponse, error) { | ||
baseUrl := conf.BaseUrl | ||
url := fmt.Sprintf("%s/routes", baseUrl) | ||
if b, err := json.Marshal(route); err != nil { | ||
return nil, err | ||
} else { | ||
if res, err := utils.Post(url, b); err != nil { | ||
return nil, err | ||
} else { | ||
var routeResp RouteResponse | ||
if err = json.Unmarshal(res, &routeResp); err != nil { | ||
return nil, err | ||
} else { | ||
if routeResp.Route.Key != nil { | ||
return &routeResp, nil | ||
} else { | ||
return nil, fmt.Errorf("apisix route not expected response") | ||
} | ||
|
||
} | ||
} | ||
} | ||
} | ||
|
||
func (route *Route) Update() (*RouteResponse, error) { | ||
// todo | ||
return nil, nil | ||
} | ||
|
||
func (route *Route) Delete() (*RouteResponse, error) { | ||
// todo | ||
return nil, nil | ||
} | ||
|
||
type RouteResponse struct { | ||
Action string `json:"action"` | ||
Route RouteNode `json:"node"` | ||
} | ||
|
||
type RouteNode struct { | ||
Key *string `json:"key"` | ||
Value Value `json:"value"` | ||
} | ||
|
||
type Value struct { | ||
UpstreamId *string `json:"upstream_id"` | ||
ServiceId *string `json:"service_id"` | ||
Plugins map[string]interface{} `json:"plugins"` | ||
Host *string `json:"host,omitempty"` | ||
Uri *string `json:"uri"` | ||
Desc *string `json:"desc"` | ||
Methods []*string `json:"methods,omitempty"` | ||
} |
Oops, something went wrong.