コード例 #1
0
class CosmosDBReplaceOfferActionTest(BaseTest):
    def setUp(self, *args, **kwargs):
        super(CosmosDBReplaceOfferActionTest, self).setUp(*args, **kwargs)
        client = local_session(Session).client('azure.mgmt.cosmosdb.CosmosDB')
        key = CosmosDBChildResource.get_cosmos_key('test_cosmosdb',
                                                   'cctestcosmosdb',
                                                   client,
                                                   readonly=False)
        self.data_client = CosmosClient(
            url_connection='https://cctestcosmosdb.documents.azure.com:443/',
            auth={'masterKey': key})

    def tearDown(self, *args, **kwargs):
        super(CosmosDBReplaceOfferActionTest, self).tearDown(*args, **kwargs)
        self.data_client.ReplaceOffer(self.initial_offer['_self'],
                                      self.initial_offer)

    def test_replace_offer_collection_action(self):

        p = self.load_policy({
            'name':
            'test-azure-cosmosdb',
            'resource':
            'azure.cosmosdb-collection',
            'filters': [{
                'type': 'value',
                'key': 'id',
                'op': 'eq',
                'value': 'cccontainer'
            }, {
                'type': 'offer',
                'key': 'content.offerThroughput',
                'op': 'eq',
                'value': 400
            }],
            'actions': [{
                'type': 'replace-offer',
                'throughput': 500
            }]
        })
        collections = p.run()
        self.assertEqual(len(collections), 1)

        self.initial_offer = collections[0]['c7n:offer'][0]
        self.collection = collections[0]
        self._assert_offer_throughput_equals(500, collections[0]['_self'])

    def _assert_offer_throughput_equals(self, throughput, resource_self):
        offers = self.data_client.ReadOffers()
        offer = next((o for o in offers if o['resource'] == resource_self),
                     None)
        self.assertIsNotNone(offer)
        self.assertEqual(offer['content']['offerThroughput'], throughput)
コード例 #2
0
class CosmosDBThroughputActionsTest(BaseTest):
    def setUp(self, *args, **kwargs):
        super(CosmosDBThroughputActionsTest, self).setUp(*args, **kwargs)
        self.client = local_session(Session).client(
            'azure.mgmt.cosmosdb.CosmosDBManagementClient')
        sub_id = local_session(Session).get_subscription_id()[-12:]
        account_name = "cctestcosmosdb%s" % sub_id
        key = CosmosDBChildResource.get_cosmos_key('test_cosmosdb',
                                                   account_name,
                                                   self.client,
                                                   readonly=False)
        self.data_client = CosmosClient(
            url_connection='https://%s.documents.azure.com:443/' %
            account_name,
            auth={'masterKey': key})
        self.offer = None

    def tearDown(self, *args, **kwargs):
        super(CosmosDBThroughputActionsTest, self).tearDown(*args, **kwargs)
        if self.offer:
            self.offer['content']['offerThroughput'] = 400
            self.data_client.ReplaceOffer(self.offer['_self'], self.offer)

    def test_replace_offer_collection_action(self):
        p = self.load_policy({
            'name':
            'test-azure-cosmosdb',
            'resource':
            'azure.cosmosdb-collection',
            'filters': [{
                'type': 'value',
                'key': 'id',
                'op': 'eq',
                'value': 'cccontainer'
            }, {
                'type': 'offer',
                'key': 'content.offerThroughput',
                'op': 'eq',
                'value': 400
            }],
            'actions': [{
                'type': 'replace-offer',
                'throughput': 500
            }]
        })
        collections = p.run()
        self.offer = collections[0]['c7n:offer']

        self.assertEqual(len(collections), 1)
        self._assert_offer_throughput_equals(500, collections[0]['_self'])

    def test_restore_throughput_state_updates_throughput_from_tag(self):

        p1 = self.load_policy({
            'name':
            'test-azure-cosmosdb',
            'resource':
            'azure.cosmosdb-collection',
            'filters': [{
                'type': 'value',
                'key': 'id',
                'op': 'eq',
                'value': 'cccontainer'
            }],
            'actions': [{
                'type': 'save-throughput-state',
                'state-tag': 'test-restore-throughput'
            }]
        })

        collections = p1.run()
        self.assertEqual(len(collections), 1)

        collection_offer = collections[0]['c7n:offer']
        self.offer = collection_offer

        throughput_to_restore = collection_offer['content']['offerThroughput']

        collection_offer['content'][
            'offerThroughput'] = throughput_to_restore + 100

        self.data_client.ReplaceOffer(collection_offer['_self'],
                                      collection_offer)

        self._assert_offer_throughput_equals(throughput_to_restore + 100,
                                             collections[0]['_self'])

        p2 = self.load_policy({
            'name':
            'test-azure-cosmosdb',
            'resource':
            'azure.cosmosdb-collection',
            'filters': [
                {
                    'type': 'value',
                    'key': 'id',
                    'op': 'eq',
                    'value': 'cccontainer'
                },
            ],
            'actions': [{
                'type': 'restore-throughput-state',
                'state-tag': 'test-restore-throughput'
            }]
        })

        collections = p2.run()

        self.assertEqual(len(collections), 1)
        self._assert_offer_throughput_equals(throughput_to_restore,
                                             collections[0]['_self'])

    def _assert_offer_throughput_equals(self, throughput, resource_self):
        self.sleep_in_live_mode()
        offers = self.data_client.ReadOffers()
        offer = next((o for o in offers if o['resource'] == resource_self),
                     None)
        self.assertIsNotNone(offer)
        self.assertEqual(throughput, offer['content']['offerThroughput'])