1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888
|
# Copyright (c) 2014-2018 VMware, Inc. All Rights Reserved.
#
# Licensed 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.
require "nokogiri"
require "test/unit"
$namespaces = %w(vim25)
def valid_ns?(t)
$namespaces.include?(t)
end
def ucfirst(v)
x = "ArrayOf"
if v.start_with?(x)
# example: ArrayOfvslmInfrastructureObjectPolicy -> ArrayOfVslm...
return x + ucfirst(v[x.length..-1])
end
# example: vslmInfrastructureObjectPolicy -. VslmInfrastructureObjectPolicy
v[0].capitalize + v[1..-1]
end
def init_type(io, name, kind)
t = "reflect.TypeOf((*#{ucfirst 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
def initialize(name)
@name = name
@children = []
end
def base?
# VrpResourceAllocationInfo is removed in 6.7, so base will no longer generated
return false if ["ResourceAllocationInfo", "FaultDomainId"].include?(@name)
return !children.empty?
end
end
@@types = {}
@@refs = {}
@@enums = {}
def self.types
return @@types
end
def self.refs
return @@refs
end
def self.enums
return @@enums
end
def self.ref(type)
refs[type] = true
end
def self.enum(type)
enums[type] = true
end
def self.enum?(type)
enums[type]
end
def self.register(name)
raise unless name
types[name] ||= Type.new(name)
end
def self.base?(name)
return unless c = types[name]
c.base?
end
def self.dump_interfaces(io)
types.keys.sort.each do |name|
next unless base?(name)
klass = types[name].klass
klass.dump_interface(io, name) if klass
end
end
end
class EnumValue
def initialize(type, value)
@type = type
@value = value
end
def type_name
ucfirst(@type.name)
end
def var_name
n = ucfirst(@type.name)
v = var_value
if v == ""
n += "Null"
else
n += ucfirst(v)
end
return n
end
def var_value
@value
end
def dump(io)
io.print "%s = %s(\"%s\")\n" % [var_name, type_name, var_value]
end
end
class Simple
include Test::Unit::Assertions
attr_accessor :name, :type
def initialize(node)
@node = node
end
def name
@name || @node["name"]
end
def type
@type || @node["type"]
end
def is_enum?
false
end
def dump_init(io)
# noop
end
def var_name
n = self.name
n = n[1..-1] if n[0] == "_" # Strip leading _
n = ucfirst(n)
return n
end
def ns(t = self.type)
t.split(":", 2)[0]
end
def vim_type?
valid_ns? ns
end
def vim_type(t = self.type)
ns, kind = t.split(":", 2)
if ! valid_ns? ns
raise
end
ucfirst(kind)
end
def base_type?
vim_type? && Peek.base?(vim_type)
end
def enum_type?
vim_type? && Peek.enum?(vim_type)
end
def any_type?
self.type == "xsd:anyType"
end
def pointer_type?
["UnitNumber"].include?(var_name) or
optional? && ["OwnerId", "GroupId", "MaxWaitSeconds", "Reservation", "Limit", "OverheadLimit", "ResourceReductionToToleratePercent"].include?(var_name)
end
def var_type
t = self.type
prefix = ""
if slice?
prefix += "[]"
if ["AffinitySet"].include?(var_name)
self.need_omitempty = false
end
end
if t =~ /^xsd:(.*)$/
t = $1
case t
when "string"
if ["IpPoolName"].include?(var_name)
self.need_omitempty = false
end
when "int"
if pointer_type?
prefix += "*"
self.need_omitempty = false
end
t = "int32"
when "boolean"
t = "bool"
if !slice? && optional?
prefix += "*"
self.need_omitempty = false
end
when "long"
if pointer_type?
prefix += "*"
self.need_omitempty = false
end
t = "int64"
when "dateTime"
t = "time.Time"
if !slice? && optional?
prefix += "*"
self.need_omitempty = false
end
when "anyType"
pkg = ""
if $target != "vim25"
pkg = "types."
end
t = "#{pkg}AnyType"
if ["Value", "Val"].include?(var_name)
self.need_omitempty = false
end
when "byte"
when "double"
t = "float64"
when "float"
t = "float32"
when "short"
t = "int16"
when "base64Binary"
t = "[]byte"
when "anyURI"
t = "string"
else
raise "unknown type: %s" % t
end
else
pkg = ""
if $target != self.ns
pkg = "types."
end
t = vim_type
if base_type?
prefix += "#{pkg}Base"
else
t = pkg + t
prefix += "*" if !slice? && !enum_type? && optional?
end
end
prefix + t
end
def slice?
test_attr("maxOccurs", "unbounded")
end
def optional?
test_attr("minOccurs", "0")
end
def need_omitempty=(v)
@need_omitempty = v
end
def need_omitempty?
var_type # HACK: trigger setting need_omitempty if necessary
if @need_omitempty.nil?
@need_omitempty = optional?
else
@need_omitempty
end
end
def need_typeattr?
base_type? || any_type?
end
protected
def test_attr(attr, expected)
actual = @node.attr(attr)
if actual != nil
case actual
when expected
true
else
raise "%s=%s" % [value, type.attr(value)]
end
else
false
end
end
end
class Element < Simple
def initialize(node)
super(node)
end
def has_type?
!@node["type"].nil?
end
def child
cs = @node.element_children
assert_equal 1, cs.length
assert_equal "complexType", cs.first.name
t = ComplexType.new(cs.first)
t.name = self.name
t
end
def dump(io)
if has_type?
io.print "type %s %s\n\n" % [ucfirst(name), var_type]
else
child.dump(io)
end
end
def dump_init(io)
if has_type?
init_type io, name, name
end
end
def dump_field(io)
tag = name
tag += ",omitempty" if need_omitempty?
tag += ",typeattr" if need_typeattr?
io.print "%s %s `xml:\"%s\"`\n" % [var_name, var_type, tag]
end
def peek(type=nil)
if has_type?
return if self.type =~ /^xsd:/
Peek.ref(vim_type)
else
child.peek()
end
end
end
class Attribute < Simple
def dump_field(io)
tag = name
tag += ",omitempty" if need_omitempty?
tag += ",attr"
io.print "%s %s `xml:\"%s\"`\n" % [var_name, var_type, tag]
end
end
class SimpleType < Simple
def is_enum?
true
end
def dump(io)
enums = @node.xpath(".//xsd:enumeration").map do |n|
EnumValue.new(self, n["value"])
end
io.print "type %s string\n\n" % ucfirst(name)
io.print "const (\n"
enums.each { |e| e.dump(io) }
io.print ")\n\n"
end
def dump_init(io)
init_type io, name, name
end
def peek
Peek.enum(name)
end
end
class ComplexType < Simple
class SimpleContent < Simple
def dump(io)
attr = Attribute.new(@node.at_xpath(".//xsd:attribute"))
attr.dump_field(io)
# HACK DELUXE(PN)
extension = @node.at_xpath(".//xsd:extension")
type = extension["base"].split(":", 2)[1]
io.print "Value %s `xml:\",chardata\"`\n" % type
end
def peek
end
end
class ComplexContent < Simple
def base
extension = @node.at_xpath(".//xsd:extension")
assert_not_nil extension
base = extension["base"]
assert_not_nil base
base
end
def dump(io)
Sequence.new(@node).dump(io, base)
end
def dump_interface(io, name)
Sequence.new(@node).dump_interface(io, name)
end
def peek
Sequence.new(@node).peek(vim_type(base))
end
end
class Sequence < Simple
def sequence
sequence = @node.at_xpath(".//xsd:sequence")
if sequence != nil
sequence.element_children.map do |n|
Element.new(n)
end
else
nil
end
end
def dump(io, base = nil)
return unless elements = sequence
if base != nil
kind = vim_type(base)
pkg = ""
if $target != ns(base)
pkg = "types."
end
io.print "#{pkg}#{kind}\n\n"
end
elements.each do |e|
e.dump_field(io)
end
end
def dump_interface(io, name)
method = "Get%s() *%s" % [name, name]
io.print "func (b *%s) %s { return b }\n" % [name, method]
io.print "type Base%s interface {\n" % name
io.print "%s\n" % method
io.print "}\n\n"
init_type io, "Base#{name}", name
end
def peek(base = nil)
return unless elements = sequence
name = @node.attr("name")
return unless name
elements.each do |e|
e.peek(name)
end
c = Peek.register(name)
if base
c.parent = base
Peek.register(c.parent).children << name
end
end
end
def klass
@klass ||= begin
cs = @node.element_children
if !cs.empty?
assert_equal 1, cs.length
case cs.first.name
when "simpleContent"
SimpleContent.new(@node)
when "complexContent"
ComplexContent.new(@node)
when "sequence"
Sequence.new(@node)
else
raise "don't know what to do for element: %s..." % cs.first.name
end
end
end
end
def dump_init(io)
init_type io, name, name
end
def dump(io)
io.print "type %s struct {\n" % ucfirst(name)
klass.dump(io) if klass
io.print "}\n\n"
end
def peek
Peek.register(name).klass = klass
klass.peek if klass
end
end
class Schema
include Test::Unit::Assertions
attr_accessor :namespace
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.
def validate_assumptions!
# Every enumeration is part of a restriction
@xml.xpath(".//xsd:enumeration").each do |n|
assert_equal "restriction", n.parent.name
end
# See type == enum
@xml.xpath(".//xsd:restriction").each do |n|
# Every restriction has type xsd:string (it's an enum)
assert_equal "xsd:string", n["base"]
# Every restriction is part of a simpleType
assert_equal "simpleType", n.parent.name
# Every restriction is alone
assert_equal 1, n.parent.element_children.size
end
# See type == complex_content
@xml.xpath(".//xsd:complexContent").each do |n|
# complexContent is child of complexType
assert_equal "complexType", n.parent.name
end
# See type == complex_type
@xml.xpath(".//xsd:complexType").each do |n|
cc = n.element_children
# OK to have an empty complexType
next if cc.size == 0
# Require 1 element otherwise
assert_equal 1, cc.size
case cc.first.name
when "complexContent"
# complexContent has 1 "extension" element
cc = cc.first.element_children
assert_equal 1, cc.size
assert_equal "extension", cc.first.name
# extension has 1 "sequence" element
ec = cc.first.element_children
assert_equal 1, ec.size
assert_equal "sequence", ec.first.name
# sequence has N "element" elements
sc = ec.first.element_children
assert sc.all? { |e| e.name == "element" }
when "simpleContent"
# simpleContent has 1 "extension" element
cc = cc.first.element_children
assert_equal 1, cc.size
assert_equal "extension", cc.first.name
# extension has 1 or more "attribute" elements
ec = cc.first.element_children
assert_not_equal 0, ec.size
assert_equal "attribute", ec.first.name
when "sequence"
# sequence has N "element" elements
sc = cc.first.element_children
assert sc.all? { |e| e.name == "element" }
else
raise "unknown element: %s" % cc.first.name
end
end
imports.each do |i|
i.validate_assumptions!
end
includes.each do |i|
i.validate_assumptions!
end
end
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
end
end
includes.each do |i|
i.types do |t|
yield t
end
end
@xml.root.children.each do |n|
case n.class.to_s
when "Nokogiri::XML::Text"
next
when "Nokogiri::XML::Element"
case n.name
when "include", "import"
next
when "element"
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"
yield ComplexType.new(n)
else
raise "unknown child: %s" % n.name
end
else
raise "unknown type: %s" % n.class
end
end
end
def imports
@imports ||= @xml.root.xpath(".//xmlns:import").map do |n|
Schema.new(WSDL.read n["schemaLocation"])
end
end
def includes
@includes ||= @xml.root.xpath(".//xmlns:include").map do |n|
Schema.new(WSDL.read n["schemaLocation"])
end
end
end
class Operation
include Test::Unit::Assertions
def initialize(wsdl, operation_node)
@wsdl = wsdl
@operation_node = operation_node
end
def name
@operation_node["name"]
end
def namespace
type = @operation_node.at_xpath("./xmlns:input").attr("message")
keep_ns(type)
end
def remove_ns(x)
ns, x = x.split(":", 2)
if ! valid_ns? ns
raise
end
x
end
def keep_ns(x)
ns, x = x.split(":", 2)
if ! valid_ns? ns
raise
end
ns
end
def find_type_for(type)
type = remove_ns(type)
message = @wsdl.message(type)
assert_not_nil message
part = message.at_xpath("./xmlns:part")
assert_not_nil message
remove_ns(part["element"])
end
def input
type = @operation_node.at_xpath("./xmlns:input").attr("message")
find_type_for(type)
end
def go_input
"types." + ucfirst(input)
end
def output
type = @operation_node.at_xpath("./xmlns:output").attr("message")
find_type_for(type)
end
def go_output
"types." + ucfirst(output)
end
def dump(io)
func = ucfirst(name)
if namespace != "vim25"
tag = "urn:#{namespace} "
end
io.print <<EOS
type #{func}Body struct{
Req *#{go_input} `xml:"urn:#{namespace} #{input},omitempty"`
Res *#{go_output} `xml:"#{tag}#{output},omitempty"`
Fault_ *soap.Fault `xml:"http://schemas.xmlsoap.org/soap/envelope/ Fault,omitempty"`
}
func (b *#{func}Body) Fault() *soap.Fault { return b.Fault_ }
EOS
io.print "func %s(ctx context.Context, r soap.RoundTripper, req *%s) (*%s, error) {\n" % [func, go_input, go_output]
io.print <<EOS
var reqBody, resBody #{func}Body
reqBody.Req = req
if err := r.RoundTrip(ctx, &reqBody, &resBody); err != nil {
return nil, err
}
return resBody.Res, nil
EOS
io.print "}\n\n"
end
end
class WSDL
attr_reader :xml
PATH = File.expand_path("../sdk", __FILE__)
def self.read(file)
File.open(File.join(PATH, file))
end
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!
schemas.each do |s|
s.validate_assumptions!
end
end
def types(&blk)
return to_enum(:types) unless block_given?
schemas.each do |s|
s.types(&blk)
end
end
def schemas
@schemas ||= @xml.xpath('.//xmlns:types/xsd:schema').map do |n|
Schema.new(n.to_xml)
end
end
def operations
@operations ||= @xml.xpath('.//xmlns:portType/xmlns:operation').map do |o|
Operation.new(self, o)
end
end
def message(type)
@messages ||= begin
h = {}
@xml.xpath('.//xmlns:message').each do |n|
h[n.attr("name")] = n
end
h
end
@messages[type]
end
def peek
types.
sort_by { |x| x.name }.
uniq { |x| x.name }.
each { |e| e.peek() }
end
def self.header(name)
return <<EOF
/*
Copyright (c) 2014-2018 VMware, Inc. All Rights Reserved.
Licensed 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 #{name}
EOF
end
end
|