Package com.cloudbees.sdk.pool

Source Code of com.cloudbees.sdk.pool.ServerPoolCreate

/*
* Copyright 2010-2014, 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.pool;

import com.cloudbees.api.BeesClient;
import com.cloudbees.api.ServerInfo;
import com.cloudbees.api.ServerPoolInfo;
import com.cloudbees.sdk.cli.BeesCommand;
import com.cloudbees.sdk.cli.CLICommand;

import java.util.List;
import java.util.Map;

/**
* @author Fabian Donze
*/
@BeesCommand(group = "Server", description = "Create a server pool")
@CLICommand("app:pool:create")
public class ServerPoolCreate extends ServerPoolBase {
    Boolean defaultOption;
    String serverSize;

    public ServerPoolCreate() {
        super();
    }

    public Boolean getDefaultOption() {
        if (defaultOption == null) {
            Map<String, String> settings = getSettings();
            String str = settings.get("default");
            if (str != null) defaultOption = Boolean.valueOf(str);
        }
        return defaultOption;
    }

    public void setDefaultOption(Boolean defaultOption) {
        this.defaultOption = defaultOption;
    }

    public String getServerSize() {
        if (serverSize == null) {
            Map<String, String> settings = getSettings();
            serverSize = settings.get("server_size");
        }
        return serverSize;
    }

    public void setServerSize(String serverSize) {
        this.serverSize = serverSize;
    }

    @Override
    protected String getUsageMessage() {
        return "POOL_NAME";
    }

    @Override
    protected boolean preParseCommandLine() {
        if (super.preParseCommandLine()) {
            addOption( "serverSize", true, "The default server size" );
            addOption( null, "default", false, "To set the pool as the default deployment pool" );
        }
        return true;
    }

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

        Map<String, String> poolSettings = getSettings();
        if (getDefaultOption() != null) {
            poolSettings.put("default", getDefaultOption().toString());
        }
        if (getServerSize() != null) {
            poolSettings.put("server_size", getServerSize());
        }

        ServerPoolInfo poolInfo = client.serverPoolCreate(getAccount(), getParameterName(), poolSettings);
        if (isTextOutput()) {
            System.out.println("Pool ID  : " + poolInfo.getId());
            System.out.println("name     : " + poolInfo.getName());
            Map<String, String> settings = poolInfo.getSettings();
            if(settings != null && settings.size() > 0) {
                System.out.println("settings:");
                for (Map.Entry<String, String> entry : settings.entrySet()) {
                    System.out.println("  " + entry.getKey() + "=" + entry.getValue());
                }
            }
            List<ServerInfo> servers = poolInfo.getServers();
            if(servers != null && servers.size() > 0) {
                System.out.println("servers:");
                for (ServerInfo server : servers) {
                    if (server.getSize() != null)
                        System.out.println("  " + server.getId() + " : " + server.getSize() + " : " + server.getState());
                    else
                        System.out.println("  " + server.getId() + " : " + server.getState());
                }
            }

        } else  printOutput(poolInfo, ServerInfo.class, ServerPoolInfo.class);

        return true;
    }
}
TOP

Related Classes of com.cloudbees.sdk.pool.ServerPoolCreate

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.