You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by sp...@apache.org on 2014/12/04 17:30:43 UTC

ambari git commit: AMBARI-8257: Simple view example with UI resources

Repository: ambari
Updated Branches:
  refs/heads/trunk 403cb0c7e -> b9d8fd039


AMBARI-8257: Simple view example with UI resources

Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/b9d8fd03
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/b9d8fd03
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/b9d8fd03

Branch: refs/heads/trunk
Commit: b9d8fd0396beda5b6f6529798c812e188359d0a9
Parents: 403cb0c
Author: sposetti <co...@yahoo.com>
Authored: Thu Dec 4 11:30:05 2014 -0500
Committer: sposetti <co...@yahoo.com>
Committed: Thu Dec 4 11:30:05 2014 -0500

----------------------------------------------------------------------
 ambari-views/examples/simple-view/docs/index.md | 93 +++++++++++++++++++
 ambari-views/examples/simple-view/pom.xml       | 96 ++++++++++++++++++++
 .../ambari/view/simple/SimpleService.java       | 73 +++++++++++++++
 .../src/main/resources/ui/index.html            | 64 +++++++++++++
 .../simple-view/src/main/resources/view.xml     | 41 +++++++++
 5 files changed, 367 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/b9d8fd03/ambari-views/examples/simple-view/docs/index.md
----------------------------------------------------------------------
diff --git a/ambari-views/examples/simple-view/docs/index.md b/ambari-views/examples/simple-view/docs/index.md
new file mode 100644
index 0000000..b750710
--- /dev/null
+++ b/ambari-views/examples/simple-view/docs/index.md
@@ -0,0 +1,93 @@
+<!---
+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.
+-->
+
+Simple View Example
+======
+
+Description
+-----
+The Simple View is an example of a basic REST service that serves the configuration parameter value,
+and a single HTML page that shows the value. It demonstrates the very basics of how
+to write and expose a REST service which works with instance parameters, and the basics of
+how to include UI assets with your view that access that service.
+
+Package
+-----
+All views are packaged as a view archive. The view archive contains the configuration
+file and various optional components of the view.
+
+#####view.xml
+
+The view.xml file is the only required file for a view archive.  The view.xml is the configuration that describes the view and view instances for Ambari.
+The view.xml for this example defines one required parameter for configuring a view instance: what.is.the.value. The view also
+defines a single resource that exposes a REST service at the /simple end point.
+
+    <view>
+      <name>SIMPLE</name>
+      <label>Simple</label>
+      <version>0.1.0</version>
+      <parameter>
+        <name>what.is.the.value</name>
+        <description>Provide a configuration value</description>
+        <required>true</required>
+      </parameter>
+      <resource>
+        <name>simple</name>
+        <service-class>org.apache.ambari.view.simple.SimpleService</service-class>
+      </resource>
+    </view>
+
+Build
+-----
+
+The view can be built as a maven project.
+
+    cd ambari-views/examples/simple-view
+    mvn clean package
+
+The build will produce the view archive.
+
+    ambari-views/examples/simple-view/target/simple-view-0.1.0.jar
+
+Place the view archive on the Ambari Server and restart to deploy.    
+
+    cp simple-view-0.1.0.jar /var/lib/ambari-server/resources/views/
+    ambari-server restart
+    
+Create View Instance
+-----
+
+With the view deployed, create an instance of the view to be used by Ambari users.
+
+    POST
+    /api/v1/views/SIMPLE/versions/0.1.0/instances/SIMPLE_1
+    
+    [ {
+      "ViewInstanceInfo" : {
+        "label" : "Simple 1",
+        "description" : "Enter a configuration value",
+        "properties" : {
+          "what.is.the.value" : "blue"
+        }
+      }
+    } ]
+
+Access the view service end point:
+
+    /api/v1/views/SIMPLE/versions/0.1.0/instances/SIMPLE_1/resources/simple
+
+Access the view UI:
+
+    /views/SIMPLE/0.1.0/SIMPLE_1

http://git-wip-us.apache.org/repos/asf/ambari/blob/b9d8fd03/ambari-views/examples/simple-view/pom.xml
----------------------------------------------------------------------
diff --git a/ambari-views/examples/simple-view/pom.xml b/ambari-views/examples/simple-view/pom.xml
new file mode 100644
index 0000000..e23faca
--- /dev/null
+++ b/ambari-views/examples/simple-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>1.0.0</version>
+  </parent>
+  <modelVersion>4.0.0</modelVersion>
+  <artifactId>simple-view</artifactId>
+  <version>0.1.0</version>
+  <packaging>jar</packaging>
+  <name>Ambari Simple View</name>
+  <url>http://maven.apache.org</url>
+  <properties>
+    <ambari.version>1.3.0-SNAPSHOT</ambari.version>
+  </properties>
+  <dependencies>
+    <dependency>
+      <groupId>org.apache.ambari</groupId>
+      <artifactId>ambari-views</artifactId>
+      <version>${ambari.version}</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/b9d8fd03/ambari-views/examples/simple-view/src/main/java/org/apache/ambari/view/simple/SimpleService.java
----------------------------------------------------------------------
diff --git a/ambari-views/examples/simple-view/src/main/java/org/apache/ambari/view/simple/SimpleService.java b/ambari-views/examples/simple-view/src/main/java/org/apache/ambari/view/simple/SimpleService.java
new file mode 100644
index 0000000..e9dcdea
--- /dev/null
+++ b/ambari-views/examples/simple-view/src/main/java/org/apache/ambari/view/simple/SimpleService.java
@@ -0,0 +1,73 @@
+/**
+ * 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.simple;
+
+import org.apache.ambari.view.ViewContext;
+
+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.util.Map;
+import java.util.HashMap;
+
+/**
+ * The Simple service.
+ */
+public class SimpleService {
+
+  private   static  final   String  PROPERTY_VALUE = "what.is.the.value";
+
+  /**
+   * 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) {
+
+    String value = context.getProperties().get(PROPERTY_VALUE);
+
+    StringBuffer buf = new StringBuffer();
+    buf.append("{\"value\" : \"");
+    buf.append(value);
+    buf.append("\"}");
+
+    return Response.ok(buf.toString()).build();
+  }
+
+} // end SimpleService

http://git-wip-us.apache.org/repos/asf/ambari/blob/b9d8fd03/ambari-views/examples/simple-view/src/main/resources/ui/index.html
----------------------------------------------------------------------
diff --git a/ambari-views/examples/simple-view/src/main/resources/ui/index.html b/ambari-views/examples/simple-view/src/main/resources/ui/index.html
new file mode 100644
index 0000000..e496613
--- /dev/null
+++ b/ambari-views/examples/simple-view/src/main/resources/ui/index.html
@@ -0,0 +1,64 @@
+<!--
+   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>Simple 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 _getSimpleResourceUri() {
+        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/simple';
+      }
+        $(document).ready(function() {
+            $('.button').click(function(){
+                $.ajax({
+                    url: _getSimpleResourceUri(),
+                    dataType: "text",
+                    success: function(data) {
+                        var json = $.parseJSON(data);
+                        $('#results').html('Value: ' + json.value);
+                    }
+                });
+            });
+        });
+    </script>
+
+</body>
+</html>
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ambari/blob/b9d8fd03/ambari-views/examples/simple-view/src/main/resources/view.xml
----------------------------------------------------------------------
diff --git a/ambari-views/examples/simple-view/src/main/resources/view.xml b/ambari-views/examples/simple-view/src/main/resources/view.xml
new file mode 100644
index 0000000..6820c9b
--- /dev/null
+++ b/ambari-views/examples/simple-view/src/main/resources/view.xml
@@ -0,0 +1,41 @@
+<!--
+   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>SIMPLE</name>
+  <label>Simple</label>
+  <version>0.1.0</version>
+  <parameter>
+    <name>what.is.the.value</name>
+    <description>Provide a configuration value</description>
+    <required>true</required>
+  </parameter>
+
+  <permission>
+    <name>READ_PERM</name>
+    <description>The read permission</description>
+  </permission>
+
+  <permission>
+    <name>SAVE_PERM</name>
+    <description>The save permission</description>
+  </permission>
+  
+  <resource>
+    <name>simple</name>
+    <service-class>org.apache.ambari.view.simple.SimpleService</service-class>
+  </resource>
+</view>
\ No newline at end of file