Skip to content

Commit

Permalink
Merge pull request #711 from dougm/pbm
Browse files Browse the repository at this point in the history
Add PBM support
  • Loading branch information
dougm authored Apr 25, 2017
2 parents 8e61e01 + d5e08cd commit 5072cda
Show file tree
Hide file tree
Showing 14 changed files with 3,181 additions and 74 deletions.
55 changes: 35 additions & 20 deletions gen/gen.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,23 +16,38 @@

set -e

if [ ! -f rbvmomi/vmodl.db ]; then
git clone https://github.com/vmware/rbvmomi
fi

dst=../vim25

pkgs=$(echo $dst/{types,methods,mo})
mkdir -p $pkgs

bundle exec ruby gen_from_wsdl.rb $dst
bundle exec ruby gen_from_vmodl.rb $dst

for p in $pkgs
do
echo $p
cd $p
goimports -w *.go
go install
cd -
done
generate() {
dst="$1"
wsdl="$2"
modl="$3"

pkgs=(types methods)
if [ -n "$modl" ] ; then
pkgs+=(mo)
fi

for p in "${pkgs[@]}"
do
mkdir -p "$dst/$p"
done

echo "generating $dst/..."

bundle exec ruby gen_from_wsdl.rb "$dst" "$wsdl"
if [ -n "$modl" ] ; then
bundle exec ruby gen_from_vmodl.rb "$dst" "$wsdl" "$modl"
fi

for p in "${pkgs[@]}"
do
pushd "$dst/$p" >/dev/null
goimports -w ./*.go
go install
popd >/dev/null
done
}

# ./sdk/ contains the contents of wsdl.zip from vimbase build 5037323

generate "../vim25" "vim" "./rbvmomi/vmodl.db" # from github.com/vmware/rbvmomi@f6907e6
generate "../pbm" "pbm"
11 changes: 4 additions & 7 deletions gen/gen_from_vmodl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,8 @@

require "test/unit"

PATH = File.expand_path("../rbvmomi", __FILE__)

def read(file)
File.open(File.join(PATH, file))
File.open(file)
end

class Prop
Expand Down Expand Up @@ -198,7 +196,6 @@ def managed
@data.map do |k,v|
next if !v.is_a?(Hash)
next if v["kind"] != "managed"
next if k =~ /^pbm/i

Managed.new(self, k, v)
end.compact
Expand All @@ -209,15 +206,15 @@ def managed
raise "first argument not a directory"
end

wsdl = WSDL.new(WSDL.read "vim.wsdl")
wsdl = WSDL.new(WSDL.read ARGV[1]+".wsdl")
wsdl.validate_assumptions!
wsdl.peek()

vmodl = Vmodl.new(read ARGV[2] || "./rbvmomi/vmodl.db")

File.open(File.join(ARGV.first, "mo/mo.go"), "w") do |io|
io.print WSDL.header("mo")

vmodl = Vmodl.new(read "vmodl.db")

vmodl.
managed.
sort_by { |m| m.name }.
Expand Down
22 changes: 21 additions & 1 deletion gen/gen_from_wsdl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@
raise "first argument not a directory"
end

wsdl = WSDL.new(WSDL.read "vim.wsdl")
target = ARGV[1]
wsdl = WSDL.new(WSDL.read target+".wsdl")
wsdl.validate_assumptions!
wsdl.peek()

Expand All @@ -41,6 +42,14 @@

File.open(File.join(ARGV.first, "types/types.go"), "w") do |io|
io.print WSDL.header("types")
if target != "vim"
io.print <<EOF
import (
"context"
"github.com/vmware/govmomi/vim25/types"
)
EOF
end

wsdl.
types.
Expand All @@ -59,6 +68,17 @@

File.open(File.join(ARGV.first, "methods/methods.go"), "w") do |io|
io.print WSDL.header("methods")
if target == "vim"
target += "25"
end

io.print <<EOF
import (
"context"
"github.com/vmware/govmomi/#{target}/types"
"github.com/vmware/govmomi/vim25/soap"
)
EOF

wsdl.
operations.
Expand Down
116 changes: 84 additions & 32 deletions gen/vim_wsdl.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,29 @@
require "nokogiri"
require "test/unit"

$namespaces = %w(vim25)

def valid_ns?(t)
$namespaces.include?(t)
end

def init_type(io, name, kind)
t = "reflect.TypeOf((*#{kind})(nil)).Elem()"

io.print "func init() {\n"

if $target == "vim25"
io.print "t[\"#{name}\"] = #{t}\n"
else
unless name.start_with? "Base"
name = "#{$target}:#{name}"
end
io.print "types.Add(\"#{name}\", #{t})\n"
end

io.print "}\n\n"
end

class Peek
class Type
attr_accessor :parent, :children, :klass
Expand Down Expand Up @@ -70,8 +93,8 @@ def self.base?(name)
def self.dump_interfaces(io)
types.keys.sort.each do |name|
next unless base?(name)

types[name].klass.dump_interface(io, name)
klass = types[name].klass
klass.dump_interface(io, name) if klass
end
end
end
Expand Down Expand Up @@ -139,17 +162,20 @@ def var_name
return n
end

def ns(t = self.type)
t.split(":", 2)[0]
end

def vim_type?
ns, _ = self.type.split(":", 2)
ns == "vim25" or ns == "internalvim25" or ns == "internalreflect"
valid_ns? ns
end

def vim_type(t = self.type)
ns, t = t.split(":", 2)
if ns != "vim25" and ns != "internalvim25" and ns != "internalreflect"
ns, kind = t.split(":", 2)
if ! valid_ns? ns
raise
end
t
kind
end

def base_type?
Expand Down Expand Up @@ -199,7 +225,11 @@ def var_type
self.need_omitempty = false
end
when "anyType"
t = "AnyType"
pkg = ""
if $target != "vim25"
pkg = "types."
end
t = "#{pkg}AnyType"
when "byte"
when "double"
t = "float64"
Expand All @@ -215,10 +245,17 @@ def var_type
raise "unknown type: %s" % t
end
else
pkg = ""
if $target != self.ns
pkg = "types."
end

t = vim_type

if base_type?
prefix += "Base"
prefix += "#{pkg}Base"
else
t = pkg + t
prefix += "*" if !slice? && !enum_type? && optional?
end
end
Expand Down Expand Up @@ -297,9 +334,7 @@ def dump(io)

def dump_init(io)
if has_type?
io.print "func init() {\n"
io.print "t[\"%s\"] = reflect.TypeOf((*%s)(nil)).Elem()\n" % [name, name]
io.print "}\n\n"
init_type io, name, name
end
end

Expand Down Expand Up @@ -347,9 +382,7 @@ def dump(io)
end

def dump_init(io)
io.print "func init() {\n"
io.print "t[\"%s\"] = reflect.TypeOf((*%s)(nil)).Elem()\n" % [name, name]
io.print "}\n\n"
init_type io, name, name
end

def peek
Expand Down Expand Up @@ -381,7 +414,7 @@ def base
base = extension["base"]
assert_not_nil base

vim_type(base)
base
end

def dump(io)
Expand All @@ -393,7 +426,7 @@ def dump_interface(io, name)
end

def peek
Sequence.new(@node).peek(base)
Sequence.new(@node).peek(vim_type(base))
end
end

Expand All @@ -411,8 +444,15 @@ def sequence

def dump(io, base = nil)
return unless elements = sequence
if base != nil
kind = vim_type(base)

io.print "%s\n\n" % base
pkg = ""
if $target != ns(base)
pkg = "types."
end
io.print "#{pkg}#{kind}\n\n"
end

elements.each do |e|
e.dump_field(io)
Expand All @@ -425,9 +465,7 @@ def dump_interface(io, name)
io.print "type Base%s interface {\n" % name
io.print "%s\n" % method
io.print "}\n\n"
io.print "func init() {\n"
io.print "t[\"Base%s\"] = reflect.TypeOf((*%s)(nil)).Elem()\n" % [name, name]
io.print "}\n\n"
init_type io, "Base#{name}", name
end

def peek(base = nil)
Expand Down Expand Up @@ -468,9 +506,7 @@ def klass
end

def dump_init(io)
io.print "func init() {\n"
io.print "t[\"%s\"] = reflect.TypeOf((*%s)(nil)).Elem()\n" % [name, name]
io.print "}\n\n"
init_type io, name, name
end

def dump(io)
Expand All @@ -488,12 +524,12 @@ def peek
class Schema
include Test::Unit::Assertions

def initialize(xml, parent = nil)
@xml = Nokogiri::XML.parse(xml)
end
attr_accessor :namespace

def targetNamespace
@xml.root["targetNamespace"]
def initialize(xml)
@xml = Nokogiri::XML.parse(xml)
@namespace = @xml.root.attr("targetNamespace").split(":", 2)[1]
@xml
end

# We have some assumptions about structure, make sure they hold.
Expand Down Expand Up @@ -578,6 +614,10 @@ def validate_assumptions!
def types
return to_enum(:types) unless block_given?

if $target != self.namespace
return
end

imports.each do |i|
i.types do |t|
yield t
Expand All @@ -599,7 +639,14 @@ def types
when "include", "import"
next
when "element"
yield Element.new(n)
e = Element.new(n)
if e.has_type? && e.vim_type?
if e.ns == $target
yield e
end
else
yield e
end
when "simpleType"
yield SimpleType.new(n)
when "complexType"
Expand Down Expand Up @@ -646,15 +693,15 @@ def namespace

def remove_ns(x)
ns, x = x.split(":", 2)
if ns != "vim25" and ns != "internalvim25" and ns != "internalreflect"
if ! valid_ns? ns
raise
end
x
end

def keep_ns(x)
ns, x = x.split(":", 2)
if ns != "vim25" and ns != "internalvim25" and ns != "internalreflect"
if ! valid_ns? ns
raise
end
ns
Expand Down Expand Up @@ -730,6 +777,11 @@ def self.read(file)

def initialize(xml)
@xml = Nokogiri::XML.parse(xml)
$target = @xml.root["targetNamespace"].split(":", 2)[1]

unless $namespaces.include? $target
$namespaces.push $target
end
end

def validate_assumptions!
Expand Down
Loading

0 comments on commit 5072cda

Please sign in to comment.