incubator-ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From vgog...@apache.org
Subject svn commit: r1182705 - in /incubator/ambari/trunk: CHANGES.txt client/src/main/java/org/apache/ambari/client/BlueprintAdd.java controller/src/main/java/org/apache/ambari/controller/rest/resources/BlueprintsResource.java src/site/apt/cli.apt
Date Thu, 13 Oct 2011 07:40:10 GMT
Author: vgogate
Date: Thu Oct 13 07:40:10 2011
New Revision: 1182705

URL: http://svn.apache.org/viewvc?rev=1182705&view=rev
Log:
AMBARI-68

Modified:
    incubator/ambari/trunk/CHANGES.txt
    incubator/ambari/trunk/client/src/main/java/org/apache/ambari/client/BlueprintAdd.java
    incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/rest/resources/BlueprintsResource.java
    incubator/ambari/trunk/src/site/apt/cli.apt

Modified: incubator/ambari/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/CHANGES.txt?rev=1182705&r1=1182704&r2=1182705&view=diff
==============================================================================
--- incubator/ambari/trunk/CHANGES.txt (original)
+++ incubator/ambari/trunk/CHANGES.txt Thu Oct 13 07:40:10 2011
@@ -2,7 +2,9 @@ Ambari Change log
 
 Release 0.1.0 - unreleased
 
-  AMBARI-67. Implement cluster list, get CLI commands
+  AMBARI-68. Implement add blueprint CLI (vgogate)
+
+  AMBARI-67. Implement cluster list, get CLI commands (vgogate)
 
   AMBARI-65. Added directory structure actions. (Eric Yang)
 

Modified: incubator/ambari/trunk/client/src/main/java/org/apache/ambari/client/BlueprintAdd.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/client/src/main/java/org/apache/ambari/client/BlueprintAdd.java?rev=1182705&r1=1182704&r2=1182705&view=diff
==============================================================================
--- incubator/ambari/trunk/client/src/main/java/org/apache/ambari/client/BlueprintAdd.java
(original)
+++ incubator/ambari/trunk/client/src/main/java/org/apache/ambari/client/BlueprintAdd.java
Thu Oct 13 07:40:10 2011
@@ -17,6 +17,147 @@
  */
 package org.apache.ambari.client;
 
-public class BlueprintAdd {
+import java.io.File;
+import java.net.MalformedURLException;
+import java.net.URI;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
 
+import javax.ws.rs.core.MediaType;
+import javax.ws.rs.core.UriBuilder;
+import javax.xml.bind.JAXBContext;
+import javax.xml.bind.Marshaller;
+import javax.xml.bind.Unmarshaller;
+
+import org.apache.ambari.common.rest.entities.Blueprint;
+import org.apache.ambari.common.rest.entities.ClusterDefinition;
+import org.apache.ambari.common.rest.entities.ClusterInformation;
+import org.apache.ambari.common.rest.entities.ClusterState;
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.GnuParser;
+import org.apache.commons.cli.HelpFormatter;
+import org.apache.commons.cli.Option;
+import org.apache.commons.cli.OptionBuilder;
+import org.apache.commons.cli.Options;
+import org.apache.commons.cli.ParseException;
+import org.codehaus.jettison.json.JSONArray;
+import org.codehaus.jettison.json.JSONObject;
+
+import com.sun.jersey.api.client.Client;
+import com.sun.jersey.api.client.ClientResponse;
+import com.sun.jersey.api.client.GenericType;
+import com.sun.jersey.api.client.WebResource;
+import com.sun.jersey.api.client.config.ClientConfig;
+import com.sun.jersey.api.client.config.DefaultClientConfig;
+
+public class BlueprintAdd extends Command {
+
+    String[] args = null;
+    Options options = null;
+   
+    CommandLine line;
+    
+    
+    public BlueprintAdd (String [] args) throws Exception {  
+        /*
+         * Build options for blueprint add
+         */
+        this.args = args;
+        addOptions();
+    }
+    
+    public void printUsage () {
+        HelpFormatter formatter = new HelpFormatter();
+        formatter.printHelp( "ambari blueprint add", this.options);
+    }
+    
+    public void addOptions () {
+             
+        Option help = new Option( "help", "Help" );
+        
+        OptionBuilder.withArgName("location");
+        OptionBuilder.isRequired();
+        OptionBuilder.hasArg();
+        OptionBuilder.withDescription( "Either URL or local file path where blueprint is
available");
+        Option location = OptionBuilder.create( "location" );
+        
+        this.options = new Options();
+        options.addOption(location);
+        options.addOption(help);
+    }
+    
+    public void parseCommandLine() {
+     
+        // create the parser
+        CommandLineParser parser = new GnuParser();
+        try {
+            // parse the command line arguments
+            line = parser.parse(this.options, this.args );
+            
+            if (line.hasOption("help")) {
+                printUsage();
+                System.exit(0);
+            }
+        }
+        catch( ParseException exp ) {
+            // oops, something went wrong
+            System.err.println( "Command parsing failed. Reason: <" + exp.getMessage()+">\n"
);
+            printUsage();
+            System.exit(-1);
+        } 
+    }
+    
+    private static URI getBaseURI() {
+        return UriBuilder.fromUri(
+                "http://localhost:4080/rest/").build();
+    }
+    
+    
+    public void run() throws Exception {
+        /* 
+         * Parse the command line to get the command line arguments
+         */
+        parseCommandLine();
+        
+        ClientConfig config = new DefaultClientConfig();
+        Client client = Client.create(config);
+        WebResource service = client.resource(getBaseURI());
+        String location = line.getOptionValue("location");
+        
+        /*
+         * Import blueprint 
+         */
+        File f = new File(location);
+        ClientResponse response = null;
+        if (!f.exists()) {
+            try {
+                URL urlx = new URL(location);
+            } catch (MalformedURLException x) {
+                System.out.println("Specified location is either a file path that does not
exists or a malformed URL");
+                System.exit(-1);
+            }
+            Blueprint bp = new Blueprint();
+            response = service.path("blueprints")
+                    .queryParam("url", location)
+                    .accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).post(ClientResponse.class,
bp);
+        } else {
+            Blueprint bp = this.readBlueprintFromFile(f);
+            response = service.path("blueprints")
+                    .accept(MediaType.APPLICATION_JSON).type(MediaType.APPLICATION_JSON).post(ClientResponse.class,
bp);
+        }        
+        if (response.getStatus() != 200) { 
+            System.err.println("Blueprint add command failed. Reason [Code: <"+response.getStatus()+">,
Message: <"+response.getHeaders().getFirst("ErrorMessage")+">]");
+            System.exit(-1);
+        }
+    }
+    
+    public Blueprint readBlueprintFromFile (File f) throws Exception {      
+        JAXBContext jc = JAXBContext.newInstance(org.apache.ambari.common.rest.entities.Blueprint.class);
+        Unmarshaller u = jc.createUnmarshaller();
+        Blueprint bp = (Blueprint)u.unmarshal(f);
+        return bp;
+    }
 }
+

Modified: incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/rest/resources/BlueprintsResource.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/rest/resources/BlueprintsResource.java?rev=1182705&r1=1182704&r2=1182705&view=diff
==============================================================================
--- incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/rest/resources/BlueprintsResource.java
(original)
+++ incubator/ambari/trunk/controller/src/main/java/org/apache/ambari/controller/rest/resources/BlueprintsResource.java
Thu Oct 13 07:40:10 2011
@@ -56,7 +56,7 @@ public class BlueprintsResource {
      */
     @POST
     @Consumes ({"application/json"})
-    public Blueprint createBlueprint(@DefaultValue("") @QueryParam("locationURL") String
locationURL, Blueprint blueprint) throws Exception {  
+    public Blueprint createBlueprint(@DefaultValue("") @QueryParam("url") String locationURL,
Blueprint blueprint) throws Exception {  
         try {
             if (locationURL == null || locationURL.equals("")) {
                 return Blueprints.getInstance().addBlueprint(blueprint);

Modified: incubator/ambari/trunk/src/site/apt/cli.apt
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/src/site/apt/cli.apt?rev=1182705&r1=1182704&r2=1182705&view=diff
==============================================================================
--- incubator/ambari/trunk/src/site/apt/cli.apt (original)
+++ incubator/ambari/trunk/src/site/apt/cli.apt Thu Oct 13 07:40:10 2011
@@ -303,9 +303,9 @@ Blueprint Commands
 
     * <<Options:>>
 
-      * <<--file>> [FILE_NAME]
+      * <<--location>> [FILE_PATH/URL]
 
-        * Specify file name of the blueprint to be imported into Ambari
+        * Specify local file path or the URL from where blueprint is to be imported into
Ambari
 
   []
 



Mime
View raw message