package com.terradue.dsi;

/*
 *  Copyright 2012 Terradue srl
 *
 *  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.
 */

import static java.lang.System.exit;

import java.util.List;

import javax.inject.Inject;

import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters;
import com.google.inject.name.Named;
import com.terradue.dsi.model.DeploymentCreation;

@Parameters( commandDescription = "Register a previously uploaded image to be run." )
public final class RegisterImage
    extends BaseTool
{

    public static void main( String[] args )
    {
        exit( new RegisterImage().execute( args ) );
    }

    @Parameter( names = { "--appliance" }, description = "The DSI applicance ID" )
    private int applianceId;

    @Parameter( names = { "--name" }, description = "The deployment name" )
    private String name;

    @Parameter( names = { "--description" }, description = "The DSI deployment description" )
    private String description;

    @Parameter( names = { "--cluster" }, description = "The DSI deployment cluster ID" )
    private String deploymentClusterId;

    @Parameter( names = { "--perf" }, description = "The performance unit" )
    private int performanceUnit;

    @Parameter( names = { "--memory" }, description = "The memory (in Mb)" )
    private int memoryMb;

    @Parameter( names = { "--virtual-cpu" }, description = "The number of virtual CPUs" )
    private int virtualCPUs;

    @Parameter( names = { "--external-ip" }, description = "Flag to use the external IP" )
    private boolean useExternalIp;

    @Parameter( names = { "--permanent-ip" }, description = "Flag to use the permanent IP" )
    private boolean permanentIp;

    @Parameter( names = { "--network" }, description = "The DSI Network ID" )
    private String networkId;

    @Parameter( names = { "--users" }, description = "The users ID allowed to use the deployment" )
    private List<String> userDelegates;

    @Parameter( names = { "--end" }, description = "The end date (in yyyy-MM-ddThh:mm:ssZ format)" )
    private String endDate;

    @Parameter( names = { "--provider" }, description = "The DSI provider ID" )
    private String providerId;

    @Parameter( names = { "--qualifier" }, description = "The DSI qualifier ID" )
    private String qualifierId;

    @Parameter( names = { "--reservation" }, description = "The DSI reservation ID" )
    private String reservationId;

    @Inject
    @Override
    public void setServiceUrl( @Named( "service.deployments" ) String serviceUrl )
    {
        super.setServiceUrl( serviceUrl );
    }

    @Override
    public void execute()
        throws Exception
    {
        logger.info( "Registering appliance {} ...", applianceId );
        restClient.resource( serviceUrl ).post( new DeploymentCreation.Builder()
                                                .withApplianceId( applianceId )
                                                .withDeploymentClusterId( deploymentClusterId )
                                                .withDescription( description )
                                                .withEndDate( endDate )
                                                .withMemoryMb( memoryMb )
                                                .withName( name )
                                                .withNetwork( networkId )
                                                .withPerformanceUnit( performanceUnit )
                                                .withPermanentIp( permanentIp )
                                                .withProviderId( providerId )
                                                .withQualifierId( qualifierId )
                                                .withReservationId( reservationId )
                                                .withUseExternalIp( useExternalIp )
                                                .withUserDelegates( userDelegates )
                                                .withVirtualCPUs( virtualCPUs )
                                                .build() );
    }

}

@@ -662,24 +662,52 @@
 stem.exit;%0A%0A
+import java.util.ArrayList;%0A
 import java.
@@ -2476,16 +2476,42 @@
 elegates
+ = new ArrayList%3CString%3E()
 ;%0A%0A    @

initialize with an empty user list (it avoid NPEs)