Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

GCP: Compute Routers #155

Merged
merged 7 commits into from
Apr 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions examples/compute/router.gyro
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
google::compute-network network-example-subnet
name: "vpc-example-subnet-routers"
description: "vpc-example-desc-subnet-for-routers-example"
routing-mode: "REGIONAL"
end

google::compute-subnet subnet-example
name: "subnet-example-routers"
description: "subnet-example-description"
ip-cidr-range: "10.0.0.0/16"
network: $(google::compute-network network-example-subnet)
region: "us-east1"
end


google::router router-example
name: "router-example"
description: "example description"
network: $(google::compute-network network-example-subnet)
region: "us-east1"

router-bgp
asn: 64512
advertise-mode: "CUSTOM"

advertised-groups: [
"ALL_SUBNETS"
]

ip-range
range: "192.168.1.0/24"
description: "example ip range updated"
end
end

router-bgp-peer
name: "ex-2"
interface-name: "if-ex-2"
peer-ip-address: "169.254.0.2"
peer-asn: 64513
advertise-mode: "DEFAULT"
advertised-route-priority: 1
end

router-interface
name: "if-ex-2"
ip-range: "169.254.0.1/30"
end

router-nat
icmp-idle-timeout-sec: 35

log-config
enable: true
filter: "ALL"
end

min-ports-per-vm: 32
name: "nats-example"
ip-allocation-option: "AUTO_ONLY"

source-subnetwork-ip-ranges-to-nat: [
"LIST_OF_SUBNETWORKS"
]

subnet
subnet: $(google::compute-subnet subnet-example)

source-ip-ranges-to-nat: [
"ALL_IP_RANGES"
]
end
end
end
147 changes: 147 additions & 0 deletions src/main/java/gyro/google/compute/RouterBgp.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
/*
* Copyright 2020, Perfect Sense, Inc.
*
* 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 gyro.google.compute;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

import gyro.core.resource.Diffable;
import gyro.core.resource.Updatable;
import gyro.core.validation.Range;
import gyro.core.validation.Required;
import gyro.core.validation.ValidStrings;
import gyro.core.validation.ValidationError;
import gyro.google.Copyable;

public class RouterBgp extends Diffable implements Copyable<com.google.api.services.compute.model.RouterBgp> {

private Long asn;
private String advertiseMode;
private List<String> advertisedGroups;
private List<RouterIpRange> ipRange;

/**
* Local BGP Autonomous System Number (ASN). Valid values belong in between ``64512`` to ``65534`` for a 16-bit ASN or between ``4200000000`` to ``4294967294`` for a 32-bit ASN. (Required)
*/
@Required
@Updatable
@Range(min = 64512, max = 65534)
@Range(min = 4200000000L, max = 4294967294L)
public Long getAsn() {
return asn;
}

public void setAsn(Long asn) {
this.asn = asn;
}

/**
* The mode to use for advertisement. Valid values are ``DEFAULT`` or ``CUSTOM``.
*/
@ValidStrings({ "DEFAULT", "CUSTOM" })
@Updatable
public String getAdvertiseMode() {
return advertiseMode;
}

public void setAdvertiseMode(String advertiseMode) {
this.advertiseMode = advertiseMode;
}

/**
* The list of prefix groups when ``advertise-mode`` is set to ``CUSTOM``. Valid values are ``ALL_SUBNETS``, ``ALL_VPC_SUBNETS`` or ``ALL_PEER_VPC_SUBNETS``.
*/
@ValidStrings({ "ALL_SUBNETS", "ALL_VPC_SUBNETS", "ALL_PEER_VPC_SUBNETS" })
@Updatable
public List<String> getAdvertisedGroups() {
if (advertisedGroups == null) {
advertisedGroups = new ArrayList<>();
}

return advertisedGroups;
}

public void setAdvertisedGroups(List<String> advertisedGroups) {
this.advertisedGroups = advertisedGroups;
}

/**
* The list of individual IP ranges when ``advertise-mode`` is set to ``CUSTOM``.
*
* @subresource gyro.google.compute.RouterIpRange
*/
@Updatable
public List<RouterIpRange> getIpRange() {
if (ipRange == null) {
ipRange = new ArrayList<>();
}

return ipRange;
}

public void setIpRange(List<RouterIpRange> ipRange) {
this.ipRange = ipRange;
}

@Override
public String primaryKey() {
return getAsn().toString();
}

@Override
public void copyFrom(com.google.api.services.compute.model.RouterBgp model) {
setAsn(model.getAsn());
setAdvertiseMode(model.getAdvertiseMode());
setAdvertisedGroups(model.getAdvertisedGroups());

if (model.getAdvertisedIpRanges() != null) {
setIpRange(model.getAdvertisedIpRanges().stream().map(i -> {
RouterIpRange routerIpRange = newSubresource(RouterIpRange.class);
routerIpRange.copyFrom(i);
return routerIpRange;
}).collect(Collectors.toList()));
}
}

com.google.api.services.compute.model.RouterBgp toRouterBgp() {
com.google.api.services.compute.model.RouterBgp routerBgp = new com.google.api.services.compute.model.RouterBgp();
routerBgp.setAsn(getAsn());
routerBgp.setAdvertiseMode(getAdvertiseMode());
routerBgp.setAdvertisedGroups(getAdvertisedGroups());
routerBgp.setAdvertisedIpRanges(getIpRange().stream()
.map(RouterIpRange::toRouterAdvertisedIpRange)
.collect(Collectors.toList()));

return routerBgp;
}

@Override
public List<ValidationError> validate(Set<String> configuredFields) {
List<ValidationError> errors = new ArrayList<>();

if (getAdvertiseMode() != null && !getAdvertiseMode().equals("CUSTOM") && (!getIpRange().isEmpty() || !getAdvertisedGroups().isEmpty())) {
errors.add(new ValidationError(
this,
null,
"'ip-ranges' and 'advertised-groups' can only be set if 'advertise-mode' is set to 'CUSTOM'"));
}

return errors;
}
}
Loading