Repository: ambari Updated Branches: refs/heads/trunk 9b1938fd6 -> 00bd07866
AMBARI-11141: Sample view for auto-cluster create that uses Ambari REST API Project: http://git-wip-us.apache.org/repos/asf/ambari/repo Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/00bd0786 Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/00bd0786 Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/00bd0786 Branch: refs/heads/trunk Commit: 00bd07866e558b97befda8296c40c7ac7c4d6a9a Parents: 9b1938f Author: sposetti <compo...@yahoo.com> Authored: Thu May 14 20:36:13 2015 -0400 Committer: sposetti <compo...@yahoo.com> Committed: Thu May 14 20:36:13 2015 -0400 ---------------------------------------------------------------------- ambari-views/examples/README.md | 4 +- .../examples/auto-cluster-view/docs/index.md | 56 +++++++++ ambari-views/examples/auto-cluster-view/pom.xml | 96 +++++++++++++++ .../view/examples/AutoClusterService.java | 116 +++++++++++++++++++ .../src/main/resources/ui/index.html | 63 ++++++++++ .../src/main/resources/view.xml | 62 ++++++++++ 6 files changed, 396 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ambari/blob/00bd0786/ambari-views/examples/README.md ---------------------------------------------------------------------- diff --git a/ambari-views/examples/README.md b/ambari-views/examples/README.md index ec1226e..650f650 100644 --- a/ambari-views/examples/README.md +++ b/ambari-views/examples/README.md @@ -23,12 +23,14 @@ See the documentation pages for the view examples. * [Hello World View](helloworld-view/docs/index.md) : Demonstrates the very basics of how to write and deploy a view in Ambari. * [Hello Servlet View](hello-servlet-view/docs/index.md) : Includes instance parameters and a servlet for a dynamic UI. +* [Hello Spring View](hello-spring-view/docs/index.md) : A very simple view example that uses Spring MVC. * [Favorite view](favorite-view/docs/index.md) : Exposes a simple resource to work with instance parameters and data. * [Calculator View](calculator-view/docs/index.md) : Includes a simple resource. * [Phone List View](phone-list-view/docs/index.md) : Demonstrates simple view persistence. * [Property View](property-view/docs/index.md) : Demonstrates view configuration property options. * [Property Validator View](property-validator-view/docs/index.md) : Demonstrates configuration property validator. -* [Weather view](weather-view/docs/index.md) +* [Weather view](weather-view/docs/index.md) : Demonstrates the the use of instance parameters, a servlet for a dynamic UI and a managed resource. +* [Auto Cluster view](auto-cluster-view/docs/index.md) : Example view that can be auto-created and configured to leverage the Ambari REST APIs to access a cluster being managed by Ambari. Please also visit the [Apache Ambari Project](http://ambari.apache.org/) page for more information. http://git-wip-us.apache.org/repos/asf/ambari/blob/00bd0786/ambari-views/examples/auto-cluster-view/docs/index.md ---------------------------------------------------------------------- diff --git a/ambari-views/examples/auto-cluster-view/docs/index.md b/ambari-views/examples/auto-cluster-view/docs/index.md new file mode 100644 index 0000000..111962c --- /dev/null +++ b/ambari-views/examples/auto-cluster-view/docs/index.md @@ -0,0 +1,56 @@ +<!--- +Licensed to the Apache Software Foundation (ASF) under one or more +contributor license agreements. See the NOTICE file distributed with +this work for additional information regarding copyright ownership. +The ASF licenses this file to You 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](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. +--> + +Auto Cluster View Example +====== + +Description +----- +The Auto Cluster View is an example of a view that can be auto-created and configured to leverage +the Ambari REST APIs to access a cluster being managed by Ambari. + +Build +----- + +The view can be built as a maven project. + + cd ambari-views/examples/auto-cluster-view + mvn clean package + +The build will produce the view archive. + + ambari-views/examples/auto-cluster-view/target/auto-cluster-view-???.jar + +Deploy +------ + +Place the view archive on the Ambari Server and start to deploy. + + cp auto-cluster-view-???.jar /var/lib/ambari-server/resources/views/ + ambari-server start + + +Cluster Association +----- + +To associate a view instance with a cluster, you need to have an Ambari managed cluster. +Use the Ambari install wizard to create a cluster that includes the YARN service. + +When the YARN service is added to the cluster a new instance of the AUTO-CLUSTER view named AUTO_CLUSTER_INSTANCE should be automatically added. + +Access the view instance end point: + + api/v1/views/AUTO-CLUSTER/versions/0.1.0/instances/AUTO_CLUSTER_INSTANCE + \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ambari/blob/00bd0786/ambari-views/examples/auto-cluster-view/pom.xml ---------------------------------------------------------------------- diff --git a/ambari-views/examples/auto-cluster-view/pom.xml b/ambari-views/examples/auto-cluster-view/pom.xml new file mode 100644 index 0000000..867fa09 --- /dev/null +++ b/ambari-views/examples/auto-cluster-view/pom.xml @@ -0,0 +1,96 @@ +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You 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. +--> +<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> + <parent> + <groupId>org.apache.ambari</groupId> + <artifactId>ambari-view-examples</artifactId> + <version>2.0.0-SNAPSHOT</version> + </parent> + <modelVersion>4.0.0</modelVersion> + <artifactId>auto-cluster-view</artifactId> + <version>0.1.0-SNAPSHOT</version> + <packaging>jar</packaging> + <name>Ambari Auto Cluster View</name> + <url>http://maven.apache.org</url> + <properties> + <ambari.dir>${project.parent.parent.parent.basedir}</ambari.dir> + </properties> + <dependencies> + <dependency> + <groupId>org.apache.ambari</groupId> + <artifactId>ambari-views</artifactId> + <version>2.0.0-SNAPSHOT</version> + </dependency> + <dependency> + <groupId>com.sun.jersey</groupId> + <artifactId>jersey-server</artifactId> + <version>1.8</version> + </dependency> + <dependency> + <groupId>javax.inject</groupId> + <artifactId>javax.inject</artifactId> + <version>1</version> + </dependency> + </dependencies> + + <build> + <pluginManagement> + <plugins> + <plugin> + <groupId>org.apache.maven.plugins</groupId> + <artifactId>maven-surefire-plugin</artifactId> + <version>2.12</version> + </plugin> + </plugins> + </pluginManagement> + <plugins> + <plugin> + <groupId>org.codehaus.mojo</groupId> + <artifactId>rpm-maven-plugin</artifactId> + <version>2.0.1</version> + <executions> + <execution> + <phase>none</phase> + <goals> + <goal>rpm</goal> + </goals> + </execution> + </executions> + </plugin> + </plugins> + <resources> + <resource> + <directory>src/main/resources/ui/</directory> + <filtering>false</filtering> + </resource> + <resource> + <directory>src/main/resources/</directory> + <filtering>false</filtering> + <includes> + <include>view.xml</include> + </includes> + </resource> + <resource> + <targetPath>WEB-INF/lib</targetPath> + <filtering>false</filtering> + <directory>target/lib</directory> + </resource> + </resources> + </build> + +</project> http://git-wip-us.apache.org/repos/asf/ambari/blob/00bd0786/ambari-views/examples/auto-cluster-view/src/main/java/org/apache/ambari/view/examples/AutoClusterService.java ---------------------------------------------------------------------- diff --git a/ambari-views/examples/auto-cluster-view/src/main/java/org/apache/ambari/view/examples/AutoClusterService.java b/ambari-views/examples/auto-cluster-view/src/main/java/org/apache/ambari/view/examples/AutoClusterService.java new file mode 100644 index 0000000..f260858 --- /dev/null +++ b/ambari-views/examples/auto-cluster-view/src/main/java/org/apache/ambari/view/examples/AutoClusterService.java @@ -0,0 +1,116 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.ambari.view.examples; + +import org.apache.ambari.view.ViewContext; +import org.apache.ambari.view.AmbariStreamProvider; +import org.apache.ambari.view.URLStreamProvider; +import org.apache.ambari.view.cluster.Cluster; +import org.apache.commons.codec.binary.Base64; +import javax.inject.Inject; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.Produces; +import javax.ws.rs.Consumes; +import javax.ws.rs.core.Context; +import javax.ws.rs.core.HttpHeaders; +import javax.ws.rs.core.Response; +import javax.ws.rs.core.UriInfo; +import java.io.IOException; +import java.io.InputStream; +import java.io.BufferedReader; +import java.io.InputStreamReader; +import java.util.Map; +import java.util.HashMap; + +import java.util.Iterator; + +/** + * The AutoClusterService service. + */ +public class AutoClusterService { + + /** + * The view context. + */ + @Inject + ViewContext context; + + /** + * Handles: GET /. + * + * @param headers http headers + * @param ui uri info + * + * @return value JSON representation + */ + @GET + @Path("/") + @Produces({"text/plain", "application/json"}) + public Response getValue(@Context HttpHeaders headers, @Context UriInfo ui) { + + StringBuilder sb = new StringBuilder(); + try { + InputStream in = null; + + // + // do I have a cluster associated with this view instance? + // + Cluster c = context.getCluster(); + if (c != null) { + AmbariStreamProvider stream = context.getAmbariStreamProvider(); + + // use the cluster name and AmbariStreamProvider to access Ambari REST API + String clusterName = c.getName(); + in = stream.readFrom("/api/v1/clusters/"+clusterName, "GET", null, null, true); + } else { + URLStreamProvider stream = context.getURLStreamProvider(); + + // use the view instance configuration to access Ambari REST API + String baseUrl = context.getProperties().get("ambari.server.url"); + String username = context.getProperties().get("ambari.server.username"); + String password = context.getProperties().get("ambari.server.password"); + + HashMap<String, String> hds = new HashMap<String, String>(); + hds.put("X-Requested-By", "auto-cluster-view"); + + String authString = username + ":" + password; + byte[] authEncBytes = Base64.encodeBase64(authString.getBytes()); + String authStringEnc = new String(authEncBytes); + + hds.put("Authorization", "Basic " + authStringEnc); + + in = stream.readFrom(baseUrl, "GET", null, hds); + } + + BufferedReader r = new BufferedReader(new InputStreamReader(in)); + String str = null; + while ((str = r.readLine()) != null) { + sb.append(str); + } + + } catch (java.io.IOException ioe) { + ioe.printStackTrace(); //!!! + } + + return Response.ok(sb.toString()).build(); + } + +} // end AutoClusterService http://git-wip-us.apache.org/repos/asf/ambari/blob/00bd0786/ambari-views/examples/auto-cluster-view/src/main/resources/ui/index.html ---------------------------------------------------------------------- diff --git a/ambari-views/examples/auto-cluster-view/src/main/resources/ui/index.html b/ambari-views/examples/auto-cluster-view/src/main/resources/ui/index.html new file mode 100644 index 0000000..d5c3a8e --- /dev/null +++ b/ambari-views/examples/auto-cluster-view/src/main/resources/ui/index.html @@ -0,0 +1,63 @@ +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You 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. +--> +<html> +<head> + <style> + body{ + text-align: center; + font-family: arial; + } + .button{ + margin:20px; + font-size:16px; + font-weight: bold; + padding:5px 10px; + } + </style> +</head> +<body> + <h1>Property Service</h1> + <br /> + <input type="button" value="Get Value" class="button" /> + <br /> + <span id="results"></span> + + <script src="http://code.jquery.com/jquery-1.11.1.min.js"></script> + + <script> + function _getPropertyResourceUri() { + var parts = window.location.pathname.match(/\/[^\/]*/g); + var view = parts[1]; + var version = '/versions' + parts[2]; + var instance = parts[3]; + return '/api/v1/views' + view + version + '/instances' + instance+'/resources/properties'; + } + $(document).ready(function() { + $('.button').click(function(){ + $.ajax({ + url: _getPropertyResourceUri(), + dataType: "text", + success: function(data) { + $('#results').html(data); + } + }); + }); + }); + </script> + +</body> +</html> \ No newline at end of file http://git-wip-us.apache.org/repos/asf/ambari/blob/00bd0786/ambari-views/examples/auto-cluster-view/src/main/resources/view.xml ---------------------------------------------------------------------- diff --git a/ambari-views/examples/auto-cluster-view/src/main/resources/view.xml b/ambari-views/examples/auto-cluster-view/src/main/resources/view.xml new file mode 100644 index 0000000..b7096ab --- /dev/null +++ b/ambari-views/examples/auto-cluster-view/src/main/resources/view.xml @@ -0,0 +1,62 @@ +<!-- + Licensed to the Apache Software Foundation (ASF) under one or more + contributor license agreements. See the NOTICE file distributed with + this work for additional information regarding copyright ownership. + The ASF licenses this file to You 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. +--> +<view> + <name>AUTO-CLUSTER</name> + <label>AutoCluster</label> + <version>0.1.0</version> + + <min-ambari-version>2.0.*</min-ambari-version> + + <parameter> + <name>ambari.server.url</name> + <description>Enter Ambari Server Cluster URL</description> + <placeholder>http://ambari.server:8080/api/v1/clusters/c1</placeholder> + <cluster-config>fake</cluster-config> + <default-value>test</default-value> + <required>true</required> + </parameter> + <parameter> + <name>ambari.server.username</name> + <description>Enter Ambari Server username</description> + <cluster-config>fake</cluster-config> + <default-value>test</default-value> + <required>true</required> + </parameter> + <parameter> + <name>ambari.server.password</name> + <description>Enter Ambari Server password</description> + <cluster-config>fake</cluster-config> + <default-value>test</default-value> + <masked>true</masked> + <required>true</required> + </parameter> + + <resource> + <name>properties</name> + <service-class>org.apache.ambari.view.examples.AutoClusterService</service-class> + </resource> + + <auto-instance> + <name>AUTO_CLUSTER_INSTANCE</name> + <label>Auto Cluster Instance View</label> + <description>This view instance is auto created when the YARN service is added to a cluster.</description> + <stack-id>HDP-2.*</stack-id> + <services> + <service>YARN</service> + </services> + </auto-instance> +</view> \ No newline at end of file