Share via


Creating an Authority Using REST (Ruby)

[This document supports a preliminary release of a software product that may be changed substantially prior to final commercial release. This document is provided for informational purposes only.]

As described in Creating an Authority Using REST, this topic provides a Ruby sample to create an authority. You will need to update the code and do the following:

  • Provide your own credentials.
  • Provide the id value for the authority being created.
  • Set proxy information.
require 'base64' 
require 'bigdecimal'
require 'net/https'
require 'openssl'
require 'rexml/document'
require 'uri'

# Provide your own credentials and sample authority id you want to create
USER_NAME = '<UserName>'
PASSWORD = '<Password>'
SAMPLE_AUTHORITY_ID = '<AuthorityIdToCreate>'
# Provide proxy information
PROXY_ADDRESS = nil 
PROXY_PORT = nil 
PROXY_USER = nil 
PROXY_PASSWORD = nil 
  
SVC_ENDPOINT = 'data.database.windows.net' 
SVC_VERSION = 'v1'

# setup credentials 
Credentials = Struct.new("Credentials", :un, :pw)
myCred = Credentials.new(USER_NAME, PASSWORD)

#setup proxy information -- if you don't have to use a proxy you can set everything to nil
Proxy_info = Struct.new("Proxy_info", :addr, :port, :user, :pw)
myProxy = Proxy_info.new(PROXY_ADDRESS, PROXY_PORT, PROXY_USER, PROXY_PASSWORD)

reqXml = REXML::Document.new()
reqXml.add_element('s:Authority')
reqXml.root().add_attribute('xmlns:s', 'https://schemas.microsoft.com/sitka/2008/03/')
reqXml.root().add_element("s:Id")
reqXml.root().elements["s:Id"].text = SAMPLE_AUTHORITY_ID


authorityUri = SAMPLE_AUTHORITY_ID + '.' + SVC_ENDPOINT
reqPath= '/' + SVC_VERSION + '/'

# Open an HTTP session to SSDS
SSDS = Net::HTTP::Proxy(myProxy.addr, myProxy.port, myProxy.user, myProxy.pw).new('data.database.windows.net', 443)
SSDS.use_ssl=true
SSDS.verify_mode=OpenSSL::SSL::VERIFY_NONE
SSDS.verify_mode=OpenSSL::SSL::VERIFY_NONE
SSDS.start()

# now we want to post to the open session
# build request to post to the open sesson
myReq = Net::HTTP::Post.new(reqPath)       
myReq['Content-Type'] = 'application/x-ssds+xml'
myReq['Content-Length'] = reqXml.to_s.size.to_s
# add to my request the basic auth 
myReq.basic_auth(myCred.un,  myCred.pw)

# send the requst 
response = SSDS.request(myReq, reqXml.to_s)

method = 'POST'
case response
  when Net::HTTPSuccess     then 
    puts SAMPLE_AUTHORITY_ID + ' created'
    error = false
  when Net::HTTPForbidden then
    puts "SSDS Access denied"
    error = true
  when Net::HTTPBadRequest then
    puts "Request is not valid"
    error = true
  when Net::HTTPConflict   then
    puts "SSDS Authority already exists"
    error = true
  else
    puts "Unexpected Error"
    error = true
  end
  if(error)
# process the http response body 
    xml = REXML::Document.new(response.body)
    puts "SSDS #{method} Error: #{xml.root().elements[1].name} =>  #{xml.root().elements[1].text}"
    puts "SSDS #{method} Error: #{xml.root().elements[2].name} =>  #{xml.root().elements[2].text}"
  end
  
  
  

See Also

Concepts

Creating an Authority Using REST
Examples of Using REST Interface with the SQL Data Services
Guidelines and Limitations