Search in sources :

Example 1 with GoogleBaseImage

use of com.netflix.spinnaker.halyard.config.model.v1.providers.google.GoogleBaseImage in project halyard by spinnaker.

the class GoogleBakeryDefaultsValidator method validate.

@Override
public void validate(ConfigProblemSetBuilder p, GoogleBakeryDefaults n) {
    DaemonTaskHandler.message("Validating " + n.getNodeName() + " with " + GoogleBakeryDefaultsValidator.class.getSimpleName());
    String zone = n.getZone();
    String network = n.getNetwork();
    String networkProjectId = n.getNetworkProjectId();
    List<GoogleBaseImage> baseImages = n.getBaseImages();
    if (StringUtils.isEmpty(zone) && StringUtils.isEmpty(network) && CollectionUtils.isEmpty(baseImages)) {
        return;
    } else if (CollectionUtils.isEmpty(credentialsList)) {
        return;
    }
    if (StringUtils.isEmpty(zone)) {
        p.addProblem(Problem.Severity.ERROR, "No zone supplied for google bakery defaults.");
    } else {
        int i = 0;
        boolean foundZone = false;
        while (!foundZone && i < credentialsList.size()) {
            GoogleNamedAccountCredentials credentials = credentialsList.get(i);
            try {
                credentials.getCompute().zones().get(credentials.getProject(), zone).execute();
                foundZone = true;
            } catch (Exception e) {
            }
            i++;
        }
        if (!foundZone) {
            p.addProblem(Problem.Severity.ERROR, "Zone " + zone + " not found via any configured google account.");
        }
    }
    if (StringUtils.isEmpty(network)) {
        p.addProblem(Problem.Severity.ERROR, "No network supplied for google bakery defaults.");
    } else {
        int j = 0;
        boolean foundNetwork = false;
        while (!foundNetwork && j < credentialsList.size()) {
            GoogleNamedAccountCredentials credentials = credentialsList.get(j);
            try {
                String project = !StringUtils.isEmpty(networkProjectId) ? networkProjectId : credentials.getProject();
                credentials.getCompute().networks().get(project, network).execute();
                foundNetwork = true;
            } catch (Exception e) {
            }
            j++;
        }
        if (!foundNetwork) {
            p.addProblem(Problem.Severity.ERROR, "Network " + network + " not found via any configured google account.");
        }
    }
    GoogleBaseImageValidator googleBaseImageValidator = new GoogleBaseImageValidator(credentialsList, halyardVersion);
    baseImages.forEach(googleBaseImage -> googleBaseImageValidator.validate(p, googleBaseImage));
}
Also used : GoogleBaseImage(com.netflix.spinnaker.halyard.config.model.v1.providers.google.GoogleBaseImage) GoogleNamedAccountCredentials(com.netflix.spinnaker.clouddriver.google.security.GoogleNamedAccountCredentials)

Example 2 with GoogleBaseImage

use of com.netflix.spinnaker.halyard.config.model.v1.providers.google.GoogleBaseImage in project halyard by spinnaker.

the class GoogleAddBaseImageCommand method buildBaseImage.

@Override
protected BaseImage buildBaseImage(String baseImageId) {
    GoogleBaseImage baseImage = new GoogleBaseImage();
    GoogleBaseImage.GoogleImageSettings imageSettings = new GoogleBaseImage.GoogleImageSettings();
    imageSettings.setImageFamily(isImageFamily);
    baseImage.setBaseImage(imageSettings);
    GoogleBaseImage.GoogleVirtualizationSettings virtualizationSettings = new GoogleBaseImage.GoogleVirtualizationSettings();
    virtualizationSettings.setSourceImage(sourceImage);
    virtualizationSettings.setSourceImageFamily(sourceImageFamily);
    baseImage.setVirtualizationSettings(virtualizationSettings);
    return baseImage;
}
Also used : GoogleBaseImage(com.netflix.spinnaker.halyard.config.model.v1.providers.google.GoogleBaseImage)

Example 3 with GoogleBaseImage

use of com.netflix.spinnaker.halyard.config.model.v1.providers.google.GoogleBaseImage in project halyard by spinnaker.

the class GoogleBaseImageValidator method validate.

@Override
public void validate(ConfigProblemSetBuilder p, GoogleBaseImage n) {
    String sourceImage = n.getVirtualizationSettings().getSourceImage();
    String sourceImageFamily = n.getVirtualizationSettings().getSourceImageFamily();
    if (StringUtils.isEmpty(sourceImage) && StringUtils.isEmpty(sourceImageFamily)) {
        p.addProblem(Problem.Severity.ERROR, "Either source image or source image family must be specified for " + n.getBaseImage().getId() + ".");
    }
    if (!StringUtils.isEmpty(sourceImage)) {
        int i = 0;
        boolean[] foundSourceImageHolder = new boolean[1];
        while (!foundSourceImageHolder[0] && i < credentialsList.size()) {
            GoogleNamedAccountCredentials credentials = credentialsList.get(i);
            List<String> imageProjects = Lists.newArrayList(credentials.getProject());
            imageProjects.addAll(credentials.getImageProjects());
            imageProjects.addAll(baseImageProjects);
            Compute compute = credentials.getCompute();
            BatchRequest imageListBatch = buildBatchRequest(compute);
            JsonBatchCallback<ImageList> imageListCallback = new JsonBatchCallback<ImageList>() {

                @Override
                public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) throws IOException {
                    p.addProblem(Problem.Severity.ERROR, "Error locating " + sourceImage + " in these projects: " + imageProjects + ": " + e.getMessage() + ".");
                }

                @Override
                public void onSuccess(ImageList imageList, HttpHeaders responseHeaders) throws IOException {
                    // No need to look through these images if the requested image was already found.
                    if (!foundSourceImageHolder[0]) {
                        if (imageList.getItems() != null) {
                            foundSourceImageHolder[0] = imageList.getItems().stream().filter(image -> image.getName().equals(sourceImage)).findFirst().isPresent();
                        }
                    }
                }
            };
            try {
                for (String imageProject : imageProjects) {
                    compute.images().list(imageProject).queue(imageListBatch, imageListCallback);
                }
                imageListBatch.execute();
            } catch (IOException e) {
                p.addProblem(Problem.Severity.ERROR, "Error locating " + sourceImage + " in these projects: " + imageProjects + ": " + e.getMessage() + ".");
            }
            i++;
        }
        if (!foundSourceImageHolder[0]) {
            p.addProblem(Problem.Severity.ERROR, "Image " + sourceImage + " not found via any configured google account.");
        }
    }
    if (!StringUtils.isEmpty(sourceImageFamily)) {
        int i = 0;
        boolean[] foundSourceImageFamilyHolder = new boolean[1];
        while (!foundSourceImageFamilyHolder[0] && i < credentialsList.size()) {
            GoogleNamedAccountCredentials credentials = credentialsList.get(i);
            List<String> imageProjects = Lists.newArrayList(credentials.getProject());
            imageProjects.addAll(credentials.getImageProjects());
            imageProjects.addAll(baseImageProjects);
            Compute compute = credentials.getCompute();
            BatchRequest imageListBatch = buildBatchRequest(compute);
            JsonBatchCallback<ImageList> imageListCallback = new JsonBatchCallback<ImageList>() {

                @Override
                public void onFailure(GoogleJsonError e, HttpHeaders responseHeaders) throws IOException {
                    p.addProblem(Problem.Severity.ERROR, "Error locating " + sourceImageFamily + " in these projects: " + imageProjects + ": " + e.getMessage() + ".");
                }

                @Override
                public void onSuccess(ImageList imageList, HttpHeaders responseHeaders) throws IOException {
                    // No need to look through these images if the requested image family was already found.
                    if (!foundSourceImageFamilyHolder[0]) {
                        if (imageList.getItems() != null) {
                            foundSourceImageFamilyHolder[0] = imageList.getItems().stream().filter(image -> sourceImageFamily.equals(image.getFamily())).findFirst().isPresent();
                        }
                    }
                }
            };
            try {
                for (String imageProject : imageProjects) {
                    compute.images().list(imageProject).queue(imageListBatch, imageListCallback);
                }
                imageListBatch.execute();
            } catch (IOException e) {
                p.addProblem(Problem.Severity.ERROR, "Error locating " + sourceImageFamily + " in these projects: " + imageProjects + ": " + e.getMessage() + ".");
            }
            i++;
        }
        if (!foundSourceImageFamilyHolder[0]) {
            p.addProblem(Problem.Severity.ERROR, "Image family " + sourceImageFamily + " not found via any configured google account.");
        }
    }
    if (StringUtils.isEmpty(n.getBaseImage().getPackageType())) {
        p.addProblem(Problem.Severity.ERROR, "Package type must be specified for " + n.getBaseImage().getId() + ".");
    }
}
Also used : GoogleNamedAccountCredentials(com.netflix.spinnaker.clouddriver.google.security.GoogleNamedAccountCredentials) HttpHeaders(com.google.api.client.http.HttpHeaders) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError) GoogleBaseImage(com.netflix.spinnaker.halyard.config.model.v1.providers.google.GoogleBaseImage) IOException(java.io.IOException) HttpRequest(com.google.api.client.http.HttpRequest) ImageList(com.google.api.services.compute.model.ImageList) EqualsAndHashCode(lombok.EqualsAndHashCode) ConfigProblemSetBuilder(com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemSetBuilder) List(java.util.List) Lists(com.google.common.collect.Lists) Validator(com.netflix.spinnaker.halyard.config.model.v1.node.Validator) Data(lombok.Data) Problem(com.netflix.spinnaker.halyard.core.problem.v1.Problem) JsonBatchCallback(com.google.api.client.googleapis.batch.json.JsonBatchCallback) BatchRequest(com.google.api.client.googleapis.batch.BatchRequest) Compute(com.google.api.services.compute.Compute) StringUtils(org.springframework.util.StringUtils) BatchRequest(com.google.api.client.googleapis.batch.BatchRequest) HttpHeaders(com.google.api.client.http.HttpHeaders) JsonBatchCallback(com.google.api.client.googleapis.batch.json.JsonBatchCallback) IOException(java.io.IOException) GoogleNamedAccountCredentials(com.netflix.spinnaker.clouddriver.google.security.GoogleNamedAccountCredentials) ImageList(com.google.api.services.compute.model.ImageList) Compute(com.google.api.services.compute.Compute) GoogleJsonError(com.google.api.client.googleapis.json.GoogleJsonError)

Aggregations

GoogleBaseImage (com.netflix.spinnaker.halyard.config.model.v1.providers.google.GoogleBaseImage)3 GoogleNamedAccountCredentials (com.netflix.spinnaker.clouddriver.google.security.GoogleNamedAccountCredentials)2 BatchRequest (com.google.api.client.googleapis.batch.BatchRequest)1 JsonBatchCallback (com.google.api.client.googleapis.batch.json.JsonBatchCallback)1 GoogleJsonError (com.google.api.client.googleapis.json.GoogleJsonError)1 HttpHeaders (com.google.api.client.http.HttpHeaders)1 HttpRequest (com.google.api.client.http.HttpRequest)1 Compute (com.google.api.services.compute.Compute)1 ImageList (com.google.api.services.compute.model.ImageList)1 Lists (com.google.common.collect.Lists)1 Validator (com.netflix.spinnaker.halyard.config.model.v1.node.Validator)1 ConfigProblemSetBuilder (com.netflix.spinnaker.halyard.config.problem.v1.ConfigProblemSetBuilder)1 Problem (com.netflix.spinnaker.halyard.core.problem.v1.Problem)1 IOException (java.io.IOException)1 List (java.util.List)1 Data (lombok.Data)1 EqualsAndHashCode (lombok.EqualsAndHashCode)1 StringUtils (org.springframework.util.StringUtils)1