Class: AzureDocumentDB::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/fluent/plugin/documentdb/client.rb

Direct Known Subclasses

PartitionedCollectionClient

Instance Method Summary collapse

Constructor Details

#initialize(master_key, url_endpoint) ⇒ Client

Returns a new instance of Client.



11
12
13
14
15
# File 'lib/fluent/plugin/documentdb/client.rb', line 11

def initialize (master_key, url_endpoint)
  @master_key = master_key
  @url_endpoint = url_endpoint
  @header = AzureDocumentDB::Header.new(@master_key)
end

Instance Method Details

#create_collection(database_resource, collection_name, colls_options = {}, custom_headers = {}) ⇒ Object



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/fluent/plugin/documentdb/client.rb', line 50

def create_collection(database_resource, collection_name, colls_options={}, custom_headers={} )
  if !database_resource 
    raise ArgumentError.new 'No database_resource!'
  end
  url = sprintf("%s/dbs/%s/colls", @url_endpoint, database_resource.database_rid )
  custom_headers['Content-Type'] = 'application/json'
  headers = @header.generate('post',
        AzureDocumentDB::RESOURCE_TYPE_COLLECTION,
        database_resource.database_rid, custom_headers )
  body = {'id' => collection_name }
  colls_options.each{|k, v|
    if k == 'indexingPolicy' || k == 'partitionKey'
      body[k] = v
    end
  }
  res = RestClient.post( url, body.to_json, headers)
  JSON.parse(res)
end

#create_database(database_name) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/fluent/plugin/documentdb/client.rb', line 17

def create_database (database_name)
  url = "#{@url_endpoint}/dbs"
  custom_headers = {'Content-Type' => 'application/json'}
  headers = @header.generate('post', AzureDocumentDB::RESOURCE_TYPE_DATABASE, '', custom_headers )
  body_json = { 'id' => database_name }.to_json
  res = RestClient.post( url, body_json, headers)
  JSON.parse(res)
end

#create_document(collection_resource, document_id, document, custom_headers = {}) ⇒ Object



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/fluent/plugin/documentdb/client.rb', line 101

def create_document(collection_resource, document_id, document, custom_headers={} )
  if !collection_resource 
    raise ArgumentError.new 'No collection_resource!'
  end
  if document['id'] && document_id != document['id'] 
    raise ArgumentError.new "Document id mismatch error (#{document_id})!"
  end
  body = { 'id' => document_id }.merge document
  url = sprintf("%s/dbs/%s/colls/%s/docs",
              @url_endpoint, collection_resource.database_rid, collection_resource.collection_rid)
  custom_headers['Content-Type'] = 'application/json'
  headers = @header.generate('post', AzureDocumentDB::RESOURCE_TYPE_DOCUMENT,
                              collection_resource.collection_rid, custom_headers )
  res = RestClient.post( url, body.to_json, headers)
  JSON.parse(res)
end

#find_collections_by_name(database_resource, collection_name) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/fluent/plugin/documentdb/client.rb', line 69

def find_collections_by_name(database_resource, collection_name)
  if !database_resource 
    raise ArgumentError.new 'No database_resource!'
  end
  ret = {}
  query_params = []
  query_text = "SELECT * FROM root r WHERE r.id=@id"
  query_params.push( {:name=>"@id", :value=> collection_name } )
  url = sprintf("%s/dbs/%s/colls", @url_endpoint, database_resource.database_rid)
  ret = _query(AzureDocumentDB::RESOURCE_TYPE_COLLECTION,
            database_resource.database_rid, url, query_text, query_params)
  ret
end

#find_databases_by_name(database_name) ⇒ Object



26
27
28
29
30
31
32
33
# File 'lib/fluent/plugin/documentdb/client.rb', line 26

def find_databases_by_name (database_name)
  query_params = []
  query_text = "SELECT * FROM root r WHERE r.id=@id"
  query_params.push( {:name=>"@id", :value=> database_name } )
  url = sprintf("%s/dbs", @url_endpoint )
  res = _query(AzureDocumentDB::RESOURCE_TYPE_DATABASE, '', url, query_text, query_params)
  res 
end

#find_documents(collection_resource, document_id, custom_headers = {}) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/fluent/plugin/documentdb/client.rb', line 118

def find_documents(collection_resource, document_id, custom_headers={})
  if !collection_resource 
    raise ArgumentError.new 'No collection_resource!'
  end
  ret = {}
  query_params = []
  query_text = "SELECT * FROM c WHERE c.id=@id"
  query_params.push( {:name=>"@id", :value=> document_id } )
  url = sprintf("%s/dbs/%s/colls/%s/docs",
          @url_endpoint, collection_resource.database_rid, collection_resource.collection_rid)
  ret = _query(AzureDocumentDB::RESOURCE_TYPE_DOCUMENT,
          collection_resource.collection_rid, url, query_text, query_params, custom_headers)
  ret
end

#get_collection_resource(database_resource, collection_name) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/fluent/plugin/documentdb/client.rb', line 83

def get_collection_resource (database_resource, collection_name)
  _collection_rid = ''
  if !database_resource 
    raise ArgumentError.new 'No database_resource!'
  end
  res = find_collections_by_name(database_resource, collection_name)
  res[:body]['DocumentCollections'].select do |col| 
    if (col['id'] == collection_name )
      _collection_rid = col['_rid']
    end
  end
  if _collection_rid.empty?
    p "no #{collection_name} collection exists"
    return nil
  end
  AzureDocumentDB::CollectionResource.new(database_resource.database_rid, _collection_rid)      
end

#get_database_resource(database_name) ⇒ Object



35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/fluent/plugin/documentdb/client.rb', line 35

def get_database_resource (database_name) 
  resource  = nil
  res = find_databases_by_name (database_name)
  if( res[:body]["_count"].to_i == 0 )
    p "no #{database_name} database exists"
    return resource
  end
  res[:body]['Databases'].select do |db| 
    if (db['id'] == database_name )
      resource = AzureDocumentDB::DatabaseResource.new(db['_rid'])
    end
  end
  resource
end

#query_documents(collection_resource, query_text, query_params, custom_headers = {}) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
# File 'lib/fluent/plugin/documentdb/client.rb', line 133

def query_documents( collection_resource, query_text, query_params, custom_headers={} )
  if !collection_resource 
    raise ArgumentError.new 'No collection_resource!'
  end
  ret = {}
  url = sprintf("%s/dbs/%s/colls/%s/docs",
          @url_endpoint, collection_resource.database_rid, collection_resource.collection_rid)
  ret = _query(AzureDocumentDB::RESOURCE_TYPE_DOCUMENT,
          collection_resource.collection_rid, url, query_text, query_params, custom_headers)
  ret
end