Class: Azure::SqlDatabaseManagement::SqlDatabaseManagementService

Inherits:
BaseManagement::BaseManagementService show all
Defined in:
lib/azure/sql_database_management/sql_database_management_service.rb

Overview

Service for managing SQL Azure servers and databases

Instance Attribute Summary

Attributes inherited from BaseManagement::BaseManagementService

#client

Instance Method Summary collapse

Methods inherited from BaseManagement::BaseManagementService

#create_affinity_group, #delete_affinity_group, #get_affinity_group, #initialize, #list_affinity_groups, #list_locations, #list_role_sizes, #update_affinity_group, #validate_configuration!

Constructor Details

This class inherits a constructor from Azure::BaseManagement::BaseManagementService

Instance Method Details

#create_server(login, password, location, version = 12.0) ⇒ Azure::SqlDatabaseManagement::SqlServer

Create a new sql server in Microsoft Azure.



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
# File 'lib/azure/sql_database_management/sql_database_management_service.rb', line 48

def create_server(, password, location, version = 12.0)
  body = Serialization.server_to_xml(, password, location, version)
  server_names = list_servers.map(&:name)
  created_server = nil

  begin
    response = client.sql_management_request(:post, servers_path, body: body, warn: true).call
    created_server = Serialization.server_name_from_xml(response, , location, version)
  rescue RuntimeError => ex
    if ex.message =~ /Please retry the request/
      created_server = list_servers.reject{|server| server_names.include?(server.name)}.first
      # sometimes the service returns 500, but it doesn't really mean it.
      #
      # If the operation returns success, the operation is complete and the server is created immediately. If the
      # operation fails because of a user error, a server is not created. If there is a communication error or
      # internal server error, you should check the status of the operation using List Servers.
      #
      raise unless created_server
    else
      raise
    end
  end
  Azure::Loggerx.info "SQL database server #{created_server.name} is created." if created_server
  created_server
end

#delete_server(name) ⇒ void

This method returns an undefined value.

Deletes the specified database server of given subscription id from Microsoft Azure.



81
82
83
84
85
# File 'lib/azure/sql_database_management/sql_database_management_service.rb', line 81

def delete_server(name)
  ensure_server_exists!(name)
  client.sql_management_request(:delete, servers_path(name)).call
  Azure::Loggerx.info "Deleted database server #{name}."
end

#delete_sql_server_firewall_rule(server_name, rule_name) ⇒ void

This method returns an undefined value.

Deletes a server-level firewall rule from a SQL Database server. Will raise Azure::SqlManagement::Error if server does not exist.



167
168
169
170
171
172
173
174
175
176
# File 'lib/azure/sql_database_management/sql_database_management_service.rb', line 167

def delete_sql_server_firewall_rule(server_name, rule_name)
  ensure_server_exists!(server_name)
  if list_sql_server_firewall_rules(server_name).any? { |rule| rule.name == rule_name }
    request_path = servers_path "#{server_name}/firewallrules/#{rule_name}"
    client.sql_management_request(:delete, request_path).call
    Azure::Loggerx.info "Deleted server-level firewall rule #{rule_name}."
  else
    raise RuleDoesNotExist.new("Firewall rule named #{rule_name} does not exist for server named #{server_name}")
  end
end

#list_serversObject

Gets a list of database servers available under the current subscription.



33
34
35
36
# File 'lib/azure/sql_database_management/sql_database_management_service.rb', line 33

def list_servers
  response = client.sql_management_request(:get, servers_path).call
  Serialization.servers_from_xml(response)
end

#list_sql_server_firewall_rules(server_name) ⇒ Array<Azure::SqlDatabaseManagement::FirewallRule>

Gets a list of server-level firewall rules set for SQL database servers. Will raise Azure::SqlManagement::Error if server does not exist.



152
153
154
155
156
# File 'lib/azure/sql_database_management/sql_database_management_service.rb', line 152

def list_sql_server_firewall_rules(server_name)
  ensure_server_exists!(server_name)
  response = client.sql_management_request(:get, servers_path("#{server_name}/firewallrules")).call
  Serialization.database_firewall_from_xml(response)
end

#reset_password(name, password) ⇒ void

This method returns an undefined value.

Sets the administrative password of a SQL Database server for a subscription. Will raise Azure::SqlManagement::Error if server does not exist.



96
97
98
99
100
101
102
103
# File 'lib/azure/sql_database_management/sql_database_management_service.rb', line 96

def reset_password(name, password)
  ensure_server_exists!(name)
  body = Serialization.reset_password_to_xml(password)
  request_path = servers_path "#{name}?op=ResetPassword"
  request = client.sql_management_request(:post, request_path, body)
  request.call
  Azure::Loggerx.info "Password for server #{name} changed successfully."
end

#set_sql_server_firewall_rule(server_name, rule_name, start_ip = public_ipv4, end_ip = public_ipv4) ⇒ void

This method returns an undefined value.

Adds a new server-level firewall or updates an existing server-level firewall rule for a SQL Database server.



119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/azure/sql_database_management/sql_database_management_service.rb', line 119

def set_sql_server_firewall_rule(server_name, rule_name, start_ip = public_ipv4, end_ip = public_ipv4)

  if [server_name, rule_name, start_ip, end_ip].any? { |arg| arg.nil? || arg.empty? || !arg.kind_of?(String) }
    raise ArgumentError.new('Missing or empty parameter server_name, rule_name, start_ip or end_ip')
  end

  fw_rule = FirewallRule.new do |rule|
    rule.name = rule_name
    rule.start_ip_address = start_ip
    rule.end_ip_address = end_ip
  end

  body = Serialization.firewall_rule_to_xml(fw_rule)
  method = if list_sql_server_firewall_rules(server_name).any? { |rule| rule.name == rule_name }
             # the rule exists, so we need to update the rule
             :put
           else
             # the rule doesn't exist, so we need to create the rule
             :post
           end
  request_path = servers_path "#{server_name}/firewallrules/#{method == :put ? rule_name : ''}"
  client.sql_management_request(method, request_path, body).call
  Azure::Loggerx.info "Modified server-level firewall rule #{rule_name}."
end