Skip to content

Commit

Permalink
[3.0]Add mesh rule route (#789)
Browse files Browse the repository at this point in the history
* add mesh rule route

* add mesh rule check
  • Loading branch information
haoyann authored Jul 29, 2021
1 parent 021c3f5 commit a9925f5
Show file tree
Hide file tree
Showing 43 changed files with 2,558 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public class Constants {
public static final String METRICS_PROTOCOL = "metrics.protocol";
public static final Set<String> CONFIGS = new HashSet<>();
public static final String COLON = ":";

public static final String MESH_RULE_SUFFIX = ".MESHAPPRULE";
static {
CONFIGS.add(WEIGHT);
CONFIGS.add(BALANCING);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@

package org.apache.dubbo.admin.common.util;

import org.apache.dubbo.common.utils.PojoUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.SafeConstructor;
import org.yaml.snakeyaml.error.YAMLException;
Expand All @@ -30,19 +30,25 @@

public class YamlParser {

private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();

public static String dumpObject(Object object) {
return new Yaml(new SafeConstructor(), new CustomRepresenter()).dumpAsMap(object);
}

public static <T> T loadObject(String content, Class<T> type) {
Map<String, Object> map = new Yaml(new SafeConstructor(), new CustomRepresenter()).load(content);
try {
return (T) PojoUtils.mapToPojo(map, type);
return OBJECT_MAPPER.convertValue(map, type);
} catch (Exception e) {
throw new YAMLException(e);
}
}

public static Iterable<Object> loadAll(String content) {
return new Yaml(new SafeConstructor(), new CustomRepresenter()).loadAll(content);
}

public static class CustomRepresenter extends Representer {

protected NodeTuple representJavaBeanProperty(Object javaBean, Property property, Object propertyValue, Tag customTag) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
/*
* 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 org.apache.dubbo.admin.controller;

import org.apache.dubbo.admin.annotation.Authority;
import org.apache.dubbo.admin.common.exception.ParamValidationException;
import org.apache.dubbo.admin.common.exception.ResourceNotFoundException;
import org.apache.dubbo.admin.common.util.Constants;
import org.apache.dubbo.admin.model.dto.MeshRouteDTO;
import org.apache.dubbo.admin.service.MeshRouteService;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@Authority(needLogin = true)
@RestController
@RequestMapping("/api/{env}/rules/route/mesh")
public class MeshRouteController {

private final MeshRouteService meshRouteService;

@Autowired
public MeshRouteController(MeshRouteService meshRouteService) {
this.meshRouteService = meshRouteService;
}

@RequestMapping(method = RequestMethod.POST)
@ResponseStatus(HttpStatus.CREATED)
public boolean createMeshRoute(@RequestBody MeshRouteDTO meshRoute, @PathVariable String env) {
String app = meshRoute.getApplication();
if (StringUtils.isEmpty(app)) {
throw new ParamValidationException("app is Empty!");
}
// todo check appName

return meshRouteService.createMeshRule(meshRoute);
}

@RequestMapping(value = "/{id}", method = RequestMethod.PUT)
public boolean updateRule(@PathVariable String id, @RequestBody MeshRouteDTO meshRoute, @PathVariable String env) {
id = id.replace(Constants.ANY_VALUE, Constants.PATH_SEPARATOR);
if (meshRouteService.findMeshRoute(id) == null) {
throw new ResourceNotFoundException("can not find mesh route, Id: " + id);
}
meshRoute.setId(id);
return meshRouteService.updateMeshRule(meshRoute);
}

@RequestMapping(method = RequestMethod.GET)
public List<MeshRouteDTO> searchRoutes(@RequestParam String application, @PathVariable String env) {
if (StringUtils.isBlank(application)) {
throw new ParamValidationException("application is required.");
}
List<MeshRouteDTO> result = new ArrayList<>();

MeshRouteDTO meshRoute = meshRouteService.findMeshRoute(application);
if (meshRoute != null) {
result.add(meshRoute);
}
return result;
}

@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public MeshRouteDTO detailRoute(@PathVariable String id, @PathVariable String env) {
id = id.replace(Constants.ANY_VALUE, Constants.PATH_SEPARATOR);
MeshRouteDTO meshRoute = meshRouteService.findMeshRoute(id);
if (meshRoute == null) {
throw new ResourceNotFoundException("Unknown ID!");
}
return meshRoute;
}

@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public boolean deleteRoute(@PathVariable String id, @PathVariable String env) {
id = id.replace(Constants.ANY_VALUE, Constants.PATH_SEPARATOR);
return meshRouteService.deleteMeshRule(id);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*
* 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 org.apache.dubbo.admin.model.dto;

public class MeshRouteDTO extends BaseDTO{

private String meshRule;

public String getMeshRule() {
return meshRule;
}

public void setMeshRule(String meshRule) {
this.meshRule = meshRule;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* 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 org.apache.dubbo.admin.model.store.mesh;

import java.util.Map;


public class BaseRule {
private String apiVersion;
private String kind;
private Map<String,String> metadata;

public String getApiVersion() {
return apiVersion;
}

public void setApiVersion(String apiVersion) {
this.apiVersion = apiVersion;
}

public String getKind() {
return kind;
}

public void setKind(String kind) {
this.kind = kind;
}

public Map<String, String> getMetadata() {
return metadata;
}

public void setMetadata(Map<String, String> metadata) {
this.metadata = metadata;
}

@Override
public String toString() {
return "BaseRule{" +
"apiVersion='" + apiVersion + '\'' +
", kind='" + kind + '\'' +
", metadata=" + metadata +
'}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* 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 org.apache.dubbo.admin.model.store.mesh;


import org.apache.dubbo.admin.model.store.mesh.destination.DestinationRule;
import org.apache.dubbo.admin.model.store.mesh.virtualservice.VirtualServiceRule;

import java.util.ArrayList;
import java.util.List;


public class VsDestinationGroup {
private String appName;
private List<VirtualServiceRule> virtualServiceRuleList = new ArrayList<>();
private List<DestinationRule> destinationRuleList = new ArrayList<>();

public String getAppName() {
return appName;
}

public void setAppName(String appName) {
this.appName = appName;
}

public List<VirtualServiceRule> getVirtualServiceRuleList() {
return virtualServiceRuleList;
}

public void setVirtualServiceRuleList(List<VirtualServiceRule> virtualServiceRuleList) {
this.virtualServiceRuleList = virtualServiceRuleList;
}

public List<DestinationRule> getDestinationRuleList() {
return destinationRuleList;
}

public void setDestinationRuleList(List<DestinationRule> destinationRuleList) {
this.destinationRuleList = destinationRuleList;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/*
* 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 org.apache.dubbo.admin.model.store.mesh.destination;


public class ConnectionPoolSettings {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* 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 org.apache.dubbo.admin.model.store.mesh.destination;


import org.apache.dubbo.admin.model.store.mesh.BaseRule;

public class DestinationRule extends BaseRule {
private DestinationRuleSpec spec;

public DestinationRuleSpec getSpec() {
return spec;
}

public void setSpec(DestinationRuleSpec spec) {
this.spec = spec;
}

@Override
public String toString() {
return "DestinationRule{" +
"base=" + super.toString() +
", spec=" + spec +
'}';
}
}
Loading

0 comments on commit a9925f5

Please sign in to comment.