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

Add support for subnetwork secondary range #220

Merged
merged 6 commits into from
Jan 18, 2022
Merged
Show file tree
Hide file tree
Changes from 5 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
49 changes: 48 additions & 1 deletion src/main/java/gyro/google/compute/SubnetworkResource.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

package gyro.google.compute;

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

import com.google.api.services.compute.Compute;
import com.google.api.services.compute.model.Operation;
Expand Down Expand Up @@ -58,6 +61,7 @@ public class SubnetworkResource extends ComputeResource implements Copyable<Subn
private String region;
private Boolean enableFlowLogs;
private Boolean privateIpGoogleAccess;
private List<SubnetworkSecondaryRange> secondaryIpRange;

// Read-only
private String id;
Expand Down Expand Up @@ -154,6 +158,24 @@ public void setPrivateIpGoogleAccess(Boolean privateIpGoogleAccess) {
this.privateIpGoogleAccess = privateIpGoogleAccess;
}

/**
* Secondary IP ranges that may be allocated for this subnet.
*
* @subresource gyro.google.compute.SubnetworkSecondaryRange
*/
@Updatable
public List<SubnetworkSecondaryRange> getSecondaryIpRange() {
if (secondaryIpRange == null) {
secondaryIpRange = new ArrayList<>();
}

return secondaryIpRange;
}

public void setSecondaryIpRange(List<SubnetworkSecondaryRange> secondaryIpRanges) {
this.secondaryIpRange = secondaryIpRanges;
}

/**
* The Id of the subnet.
*/
Expand Down Expand Up @@ -193,6 +215,16 @@ public void copyFrom(Subnetwork subnetwork) {
subnetwork.getNetwork()));
setRegion(subnetwork.getRegion().substring(subnetwork.getRegion().lastIndexOf("/") + 1));
setSelfLink(subnetwork.getSelfLink());

getSecondaryIpRange().clear();
List<com.google.api.services.compute.model.SubnetworkSecondaryRange> secondaryIpRanges = subnetwork.getSecondaryIpRanges();
if (secondaryIpRanges != null) {
secondaryIpRanges.forEach(ipRange -> {
SubnetworkSecondaryRange secondaryRange = newSubresource(SubnetworkSecondaryRange.class);
secondaryRange.copyFrom(ipRange);
getSecondaryIpRange().add(secondaryRange);
});
}
}

@Override
Expand All @@ -217,6 +249,12 @@ public void doCreate(GyroUI ui, State state) throws Exception {
subnetwork.setEnableFlowLogs(getEnableFlowLogs());
subnetwork.setPrivateIpGoogleAccess(getPrivateIpGoogleAccess());

if (!getSecondaryIpRange().isEmpty()) {
subnetwork.setSecondaryIpRanges(getSecondaryIpRange().stream()
.map(SubnetworkSecondaryRange::toSecondaryIpRange)
.collect(Collectors.toList()));
}

Compute.Subnetworks.Insert insert = client.subnetworks().insert(getProjectId(), getRegion(), subnetwork);
Operation operation = insert.execute();
waitForCompletion(client, operation);
Expand All @@ -242,6 +280,15 @@ public void doUpdate(GyroUI ui, State state, Resource current, Set<String> chang
operation = client.subnetworks().setPrivateIpGoogleAccess(getProjectId(), getRegion(), getName(), flag).execute();
waitForCompletion(client, operation);
}

if (changedFieldNames.contains("secondary-ip-range")) {
Subnetwork subnetwork = client.subnetworks().get(getProjectId(), getRegion(), getName()).execute();
subnetwork.setSecondaryIpRanges(getSecondaryIpRange().stream()
.map(SubnetworkSecondaryRange::toSecondaryIpRange)
.collect(Collectors.toList()));
operation = client.subnetworks().patch(getProjectId(), getRegion(), getName(), subnetwork).execute();
waitForCompletion(client, operation);
}
}

@Override
Expand All @@ -251,4 +298,4 @@ public void doDelete(GyroUI ui, State state) throws Exception {
Operation operation = client.subnetworks().delete(getProjectId(), getRegion(), getName()).execute();
waitForCompletion(client, operation);
}
}
}
72 changes: 72 additions & 0 deletions src/main/java/gyro/google/compute/SubnetworkSecondaryRange.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2022, Brightspot.
*
* 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 gyro.core.resource.Diffable;
import gyro.core.validation.Required;
import gyro.google.Copyable;

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

private String ipCidrRange;
private String name;

/**
* The cidr for the ip range.
*/
@Required
public String getIpCidrRange() {
return ipCidrRange;
}

public void setIpCidrRange(String ipCidrRange) {
this.ipCidrRange = ipCidrRange;
}

/**
* The name for the ip range.
*/
@Required
public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

@Override
public String primaryKey() {
return getName();
}

@Override
public void copyFrom(com.google.api.services.compute.model.SubnetworkSecondaryRange model) {
setName(model.getRangeName());
setIpCidrRange(model.getIpCidrRange());
}

protected com.google.api.services.compute.model.SubnetworkSecondaryRange toSecondaryIpRange() {
com.google.api.services.compute.model.SubnetworkSecondaryRange gcpRange =
new com.google.api.services.compute.model.SubnetworkSecondaryRange();

gcpRange.setIpCidrRange(getIpCidrRange());
gcpRange.setRangeName(getName());

return gcpRange;
}
}