Skip to content
This repository has been archived by the owner on Dec 2, 2022. It is now read-only.

Commit

Permalink
Merge pull request #47 from hashicorp/f-011-api
Browse files Browse the repository at this point in the history
support for nomad 0.11.1 API
  • Loading branch information
cgbaker authored May 12, 2020
2 parents bf7ca7b + 7d6a58b commit 91142b4
Show file tree
Hide file tree
Showing 48 changed files with 3,120 additions and 37 deletions.
6 changes: 6 additions & 0 deletions generator/generate.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
#! /bin/bash
# Generates the API model JavaBeans using reflection on the golang nomad/api package

set -euo pipefail

GO111MODULE=on go run *.go ../sdk/src/main/java/com/hashicorp/nomad/apimodel/
63 changes: 51 additions & 12 deletions generator/generator.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,9 +152,10 @@ func (generator *Generator) generateClass(t reflect.Type) error {
if t.Field(i).Name == "WriteMeta" || t.Field(i).Name == "QueryMeta" {
continue
}
property := generator.javaBeanProperty(t.Field(i))
properties = append(properties, property)
collectTypes(property.JavaType)
if property, ok := generator.javaBeanProperty(className, t.Field(i)); ok {
properties = append(properties, property)
collectTypes(property.JavaType)
}
}

out, err := os.Create(className + ".java")
Expand Down Expand Up @@ -216,19 +217,24 @@ func (generator *Generator) generateClass(t reflect.Type) error {
return nil
}

func (g *Generator) javaBeanProperty(f reflect.StructField) java.BeanProperty {
func (g *Generator) javaBeanProperty(className string, f reflect.StructField) (java.BeanProperty,bool) {
nameInJson := f.Tag.Get("json")
if i := strings.IndexByte(nameInJson, ','); i >= 0 {
nameInJson = nameInJson[0:i]
}
if nameInJson == "-" {
return java.BeanProperty{}, false
}
if nameInJson == "" {
nameInJson = f.Name
}
propName := propertyName(nameInJson)
return java.BeanProperty{
Name: propertyName(nameInJson),
JavaType: g.javaType(f.Type),
Name: propName,
MethodSuffix: methodSuffix(nameInJson),
JavaType: g.javaType(className, propName, f.Type),
GetterAnnotation: "@JsonProperty(\"" + nameInJson + "\")",
}
}, true
}

func className(t reflect.Type) string {
Expand All @@ -238,7 +244,17 @@ func className(t reflect.Type) string {
// To meet JavaBean conventions, lowercase the first letter of the name
// and lowercase the non-first letters of multi-letter acronyms
func propertyName(nameInJson string) string {
return javaName(nameInJson, false)
raw := javaName(nameInJson, false)
if java.IsTypeName(raw) {
raw += "Val"
}
return raw
}

// methodSuffix is prefixed by get- and set- in the getter and the setter
// it is therefore less restrictive than propertyName
func methodSuffix(nameInJson string) string {
return javaName(nameInJson, true)
}

func javaName(nameInJson string, firstCharacterUppercase bool) string {
Expand Down Expand Up @@ -267,7 +283,11 @@ func javaName(nameInJson string, firstCharacterUppercase bool) string {
}
}

func (generator *Generator) javaType(t reflect.Type) java.JavaType {
func (generator *Generator) javaType(class, property string, t reflect.Type) java.JavaType {
if typ, ok := typeHack(class, property); ok {
return typ
}

if t.PkgPath() == "" {
switch t.Kind() {
case reflect.Bool:
Expand Down Expand Up @@ -311,7 +331,9 @@ func (generator *Generator) javaType(t reflect.Type) java.JavaType {
panic("Unknown interface to convert to Java" + t.String())
}
case reflect.Map:
return java.NewMapType(generator.javaType(t.Key()), generator.javaType(t.Elem()))
return java.NewMapType(
generator.javaType(class, property, t.Key()),
generator.javaType(class, property, t.Elem()))
case reflect.Ptr:
switch t.Elem().Kind() {
case reflect.Bool:
Expand All @@ -328,7 +350,7 @@ func (generator *Generator) javaType(t reflect.Type) java.JavaType {
case reflect.String:
return java.String
case reflect.Struct:
return generator.javaType(t.Elem())
return generator.javaType(class, property, t.Elem())
case reflect.Uint64:
// the only unsigned type in Java is byte, use wider type
return java.BigInteger
Expand All @@ -339,7 +361,7 @@ func (generator *Generator) javaType(t reflect.Type) java.JavaType {
if t.Elem().Kind() == reflect.Uint8 {
return java.NewReferenceType("byte[]")
} else {
return java.NewListType(generator.javaType(t.Elem()))
return java.NewListType(generator.javaType(class, property, t.Elem()))
}
case reflect.String:
return java.String
Expand All @@ -364,8 +386,25 @@ func (generator *Generator) javaType(t reflect.Type) java.JavaType {
return java.String
case reflect.Int:
return java.Integer
case reflect.Map:
return java.NewMapType(
generator.javaType(class, property, t.Key()),
generator.javaType(class, property, t.Elem()))
default:
panic("Unknown kind " + t.Kind().String() + " for " + t.String() + " in package " + t.PkgPath())
}
}
}

func typeHack(className, propertyName string) (java.JavaType, bool) {
// these are timestamps from Consul's autopilot code, which encode times differently than the rest of Nomad's API
if className == "ServerHealth" && propertyName == "lastContact" {
return java.String, true
} else if className == "AutopilotConfiguration" {
if propertyName == "lastContactThreshold" || propertyName == "serverStabilizationTime" {
return java.String, true
}
}

return nil, false
}
5 changes: 5 additions & 0 deletions generator/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module github.com/hashicorp/nomad-java-sdk/generator

go 1.14

require github.com/hashicorp/nomad/api v0.0.0-20200512134002-a12cfc806065
35 changes: 35 additions & 0 deletions generator/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/docker/go-units v0.3.3 h1:Xk8S3Xj5sLGlG5g67hJmYMmUgXv5N4PhkjJHHqrwnTk=
github.com/docker/go-units v0.3.3/go.mod h1:fgPhTUdO+D/Jk86RDLlptpiXQzgHJF7gydDDbaIK4Dk=
github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75 h1:f0n1xnMSmBLzVfsMMvriDyA75NB/oBgILX2GcHXIQzY=
github.com/gorhill/cronexpr v0.0.0-20180427100037-88b0669f7d75/go.mod h1:g2644b03hfBX9Ov0ZBDgXXens4rxSxmqFBbhvKv2yVA=
github.com/gorilla/websocket v1.4.1 h1:q7AeDBpnBk8AogcD4DSag/Ukw/KV+YhzLj2bP5HvKCM=
github.com/gorilla/websocket v1.4.1/go.mod h1:YR8l580nyteQvAITg2hZ9XVh4b55+EU/adAjf1fMHhE=
github.com/hashicorp/go-cleanhttp v0.5.1 h1:dH3aiDG9Jvb5r5+bYHsikaOUIpcM0xvgMXVoDkXMzJM=
github.com/hashicorp/go-cleanhttp v0.5.1/go.mod h1:JpRdi6/HCYpAwUzNwuwqhbovhLtngrth3wmdIIUrZ80=
github.com/hashicorp/go-rootcerts v1.0.2 h1:jzhAVGtqPKbwpyCPELlgNWhE1znq+qwJtW5Oi2viEzc=
github.com/hashicorp/go-rootcerts v1.0.2/go.mod h1:pqUvnprVnM5bf7AOirdbb01K4ccR319Vf4pU3K5EGc8=
github.com/hashicorp/nomad/api v0.0.0-20200512134002-a12cfc806065 h1:nj+XKX1cwCFnjtNLZynzm737GVdFDyTbBe5SDqE6zFw=
github.com/hashicorp/nomad/api v0.0.0-20200512134002-a12cfc806065/go.mod h1:QqvoCwX0S1cAwfN21AbIqTGL+WBB5/tFxkMzF8tM/zg=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0 h1:45sCR5RtlFHMR4UwH9sdQ5TC8v0qDQCHnXt+kaKSTVE=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
github.com/mitchellh/go-homedir v1.1.0 h1:lukF9ziXFxDFPkA1vsr5zpc1XuPDn/wFntq5mG+4E0Y=
github.com/mitchellh/go-homedir v1.1.0/go.mod h1:SfyaCUpYCn1Vlf4IUYiD9fPX4A5wJrkLzIz1N1q0pr0=
github.com/mitchellh/go-testing-interface v1.0.0 h1:fzU/JVNcaqHQEcVFAKeR41fkiLdIPrefOvVG1VZ96U0=
github.com/mitchellh/go-testing-interface v1.0.0/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.5.1 h1:nOGnQDM7FYENwehXlg/kFVnos3rEvtKTjRvOWSzb6H4=
github.com/stretchr/testify v1.5.1/go.mod h1:5W2xD1RspED5o8YsWQXVCued0rvSQ+mT+I5cxcmMvtA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.2.8 h1:obN1ZagJSUGI0Ek/LBmuj4SNLPfIny3KsKFopxRdj10=
gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
4 changes: 4 additions & 0 deletions generator/java/beanproperty.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,13 @@ type BeanProperty struct {
Name string
JavaType JavaType
GetterAnnotation string
MethodSuffix string
}

func (p *BeanProperty) methodSuffix() string {
if p.MethodSuffix != "" {
return p.MethodSuffix
}
return strings.ToUpper(p.Name[0:1]) + p.Name[1:]
}

Expand Down
17 changes: 17 additions & 0 deletions generator/java/lang.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ func IsKeyword(s string) bool {
return true
case "public":
return true
case "native":
return true
default:
return false
}
}

func IsTypeName(s string) bool {
switch s {
case "int":
return true
case "string":
return true
case "float":
return true
case "bool":
return true
default:
return false
}
Expand Down
4 changes: 2 additions & 2 deletions scripts/build-nomad.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#!/usr/bin/env bash
set -euo pipefail

nomad_version=v0.10.5
go_version=1.12
nomad_version=v0.11.1
go_version=1.14

cd "$(dirname "$0")/.."

Expand Down
9 changes: 0 additions & 9 deletions scripts/generate.sh

This file was deleted.

4 changes: 0 additions & 4 deletions sdk/src/main/java/com/hashicorp/nomad/apimodel/Attribute.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,6 @@
* @see <a href="https://www.nomadproject.io/docs/http/index.html">Nomad HTTP API</a> documentation associated with the endpoint you are using.
*/
public final class Attribute extends ApiObject {
/**
* The generator is going to make a mess of these, because the JSON names collide with Java types.
* This file will need to be manually restored.
*/
private double floatVal;
private Long intVal;
private String stringVal;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,10 @@
*/
public final class AutopilotConfiguration extends ApiObject {
private boolean cleanupDeadServers;
private String lastContactThreshold; // NOTE: the API generator will try to convert this to long but
// the Nomad HTTP API emits it as a string; do not change
private String lastContactThreshold;
private BigInteger maxTrailingLogs;
private String serverStabilizationTime; // NOTE: the API generator will try to convert this to long but
// the Nomad HTTP API emits it as a string; do not change
private long minQuorum;
private String serverStabilizationTime;
private boolean enableRedundancyZones;
private boolean disableUpgradeMigration;
private boolean enableCustomUpgrades;
Expand Down Expand Up @@ -56,6 +55,16 @@ public AutopilotConfiguration setMaxTrailingLogs(BigInteger maxTrailingLogs) {
return this;
}

@JsonProperty("MinQuorum")
public long getMinQuorum() {
return minQuorum;
}

public AutopilotConfiguration setMinQuorum(long minQuorum) {
this.minQuorum = minQuorum;
return this;
}

@JsonProperty("ServerStabilizationTime")
public String getServerStabilizationTime() {
return serverStabilizationTime;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
* @see <a href="https://www.nomadproject.io/docs/http/index.html">Nomad HTTP API</a> documentation associated with the endpoint you are using.
*/
public final class ConsulConnect extends ApiObject {
private boolean connectNative;
private boolean Native;
private ConsulSidecarService sidecarService;
private SidecarTask sidecarTask;

@JsonProperty("Native")
public boolean getNative() {
return connectNative;
return Native;
}

public ConsulConnect setNative(boolean connectNative) {
this.connectNative = connectNative;
public ConsulConnect setNative(boolean Native) {
this.Native = Native;
return this;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.hashicorp.nomad.apimodel;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.hashicorp.nomad.javasdk.ApiObject;
import com.hashicorp.nomad.javasdk.NomadJson;

import java.io.IOException;
import java.util.List;

/**
* This is a generated JavaBean representing a request or response structure.
*
* @see <a href="https://www.nomadproject.io/docs/http/index.html">Nomad HTTP API</a> documentation associated with the endpoint you are using.
*/
public final class ConsulExposeConfig extends ApiObject {
private List<ConsulExposePath> path;

@JsonProperty("Path")
public List<ConsulExposePath> getPath() {
return path;
}

public ConsulExposeConfig setPath(List<ConsulExposePath> path) {
this.path = path;
return this;
}

public ConsulExposeConfig addPath(ConsulExposePath... path) {
if (this.path == null)
this.path = new java.util.ArrayList<>();
for (ConsulExposePath item : path)
this.path.add(item);
return this;
}

@Override
public String toString() {
return NomadJson.serialize(this);
}

public static ConsulExposeConfig fromJson(String json) throws IOException {
return NomadJson.deserialize(json, ConsulExposeConfig.class);
}

public static List<ConsulExposeConfig> fromJsonArray(String json) throws IOException {
return NomadJson.deserializeList(json, ConsulExposeConfig.class);
}
}
Loading

0 comments on commit 91142b4

Please sign in to comment.