All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.cloudbees.sdk.commands.config.ConfigParametersUnSet Maven / Gradle / Ivy

/*
 * Copyright 2010-2013, CloudBees 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 com.cloudbees.sdk.commands.config;


import com.cloudbees.api.BeesClient;
import com.cloudbees.api.ConfigurationParametersResponse;
import com.cloudbees.api.ConfigurationParametersUpdateResponse;
import com.cloudbees.sdk.cli.BeesCommand;
import com.cloudbees.sdk.cli.CLICommand;
import com.cloudbees.sdk.commands.config.model.ConfigParameters;
import com.cloudbees.sdk.commands.config.model.Environment;
import com.cloudbees.sdk.commands.config.model.ResourceSettings;

import java.io.File;
import java.io.FileWriter;
import java.util.List;

/**
 * @author Fabian Donze
 */
@BeesCommand(group="Configuration")
@CLICommand("config:unset")
public class ConfigParametersUnSet extends ConfigParametersBase {
    private String environment;
    private String name;

    public ConfigParametersUnSet() {
    }

    public String getEnvironment() {
        return environment;
    }

    public void setEnvironment(String environment) {
        this.environment = environment;
    }

    public String getName() {
        return name;
    }

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

    @Override
    protected String getUsageMessage() {
        return "[name]";
    }

    @Override
    protected boolean preParseCommandLine() {
        if (super.preParseCommandLine()) {
            addOption( "e", "environment", true, "Optional environment scope (only for account parameters)", true);
            addOption( "n", "name", true, "Optional resource name to unset a specific resource parameter", true);
            return true;
        }
        return false;
    }

    @Override
    protected boolean execute() throws Exception {
        BeesClient client = getBeesClient(BeesClient.class);

        String resourceId;
        String resourceType;
        if (isGlobal("unset")) {
            resourceId = getAccount();
            resourceType = "global";
        } else {
            resourceId = getAppId();
            resourceType = "application";
        }
        ConfigurationParametersResponse res = client.configurationParameters(resourceId, resourceType);

        ConfigParameters configParameters = ConfigParameters.parse(res.getConfiguration());

        List otherArgs = getParameters();

        if (otherArgs.size() > 0) {
            for (String name : otherArgs) {
                if (getEnvironment() != null) {
                    Environment environment = configParameters.getEnvironment(getEnvironment());
                    deleteParameter(configParameters, name, getName(), environment);
                    deleteRuntimeParameter(configParameters, name, environment);
                } else {
                    deleteParameter(configParameters, name, getName(), null);
                    deleteRuntimeParameter(configParameters, name, null);
                }
            }
        } else if (getName() != null) {
            if (getEnvironment() != null) {
                Environment environment = configParameters.getEnvironment(getEnvironment());
                deleteParameter(configParameters, getName(), null, environment);
            } else {
                deleteParameter(configParameters, getName(), null, null);
            }
        } else if (getEnvironment() != null) {
            configParameters.deleteEnvironment(getEnvironment());
        }

        File xmlFile = File.createTempFile("conf", "xml");
        xmlFile.deleteOnExit();
        FileWriter fos = null;
        try {
            fos = new FileWriter(xmlFile);
            fos.write(configParameters.toXML());
        } finally {
            if (fos != null)
                fos.close();
        }

        ConfigurationParametersUpdateResponse res2 = client.configurationParametersUpdate(resourceId, resourceType, xmlFile);
        if (isTextOutput()) {
            displayStatus(resourceType, resourceId, res2.getStatus());
        } else {
            printOutput(res2, ConfigurationParametersUpdateResponse.class);
        }

        printConfigParameters(configParameters);

        return true;
    }

    private void deleteParameter(ConfigParameters configParameters, String parameterName, String resourceName, Environment environment) {
        if (resourceName != null) {
            ResourceSettings resource;
            if (environment != null) {
                resource = environment.getResource(resourceName);
            } else {
                resource = configParameters.getResource(resourceName);
            }
            if (resource != null) {
                resource.deleteParameter(parameterName);
            }
        } else {
            if (environment != null) {
                environment.deleteResource(parameterName);
                environment.deleteParameter(parameterName);
            } else {
                configParameters.deleteResource(parameterName);
                configParameters.deleteParameter(parameterName);
            }
        }
    }

    private void deleteRuntimeParameter(ConfigParameters configParameters, String parameterName, Environment environment) {
        if (environment != null) {
            environment.deleteRuntimeParameter(parameterName);
        } else {
            configParameters.deleteRuntimeParameter(parameterName);
        }
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy