def search(searchTerm): """ Print recent tweets containing `searchTerm`. """ api = twitter.Api() tweets = api.GetSearch(searchTerm) for tweet in tweets: util.safe_print(tweet.GetText())
def userTweets(username): """ Print recent tweets by `username`. pass #pass is a placeholder for where the code will go, so it doesn't break rest of code when run """ api = twitter.Api() user_tweets = api.GetUserTimeline(username) for tweet in user_tweets: util.safe_print(tweet.GetText())
def trendingTopics(): """ Print the currently trending topics. """ api = twitter.Api() trending_topics = api.GetTrendsWoeid(PHILA_WOEID) for topic in trending_topics: util.safe_print(topic.name)
def trendingTopics(): """ Print the currently trending topics. To test this function on terminal, python twitter_api.py -t """ api = twitter.Api(consumer_key=key,consumer_secret=secret,access_token_key=access_key,access_token_secret=access_secret) trending_topics = api.GetTrendsWoeid(BOSTON_WOEID) for topic in trending_topics: util.safe_print(topic.name)
def trendingTweets(): """ Print tweets for all the trending topics. To test this function, at the command line run: python twitter_api.py -w """ api = twitter.Api(consumer_key=key,consumer_secret=secret,access_token_key=access_key,access_token_secret=access_secret) trending_topics = api.GetTrendsWoeid(BOSTON_WOEID) for tweet in trending_topics: util.safe_print(tweet.GetText())
def trendingTopics(): """ Print the currently trending topics. To test this function, at the command line run: python twitter_api.py -t """ trending_topics = api.GetTrendsWoeid(BOSTON_WOEID) for topic in trending_topics: util.safe_print(topic.name)
def trendingTweets(): """ Print tweets for all the trending topics. """ api = twitter.Api() trending_topics = api.GetTrendsWoeid(PHILA_WOEID) for topic in trending_topics: topicSearchTerm = topic.name trending_tweets = api.GetSearch(topicSearchTerm) for tweet in trending_tweets: util.safe_print(tweet.GetText())
def trendingTweets(): """ Print tweets for all the trending topics. To test this function, at the command line run: python twitter_api.py -w """ api = twitter.Api(consumer_key=key,consumer_secret=secret,access_token_key=access_key,access_token_secret=access_secret) trending = api.GetTrendsCurrent() for topic in trending: util.safe_print(topic.name)
def trendingTopics(): """ Print the currently trending topics. To test this function, at the command line run: python twitter_api.py -t """ api = twitter.Api(consumer_key=key,consumer_secret=secret,access_token_key=access_key,access_token_secret=access_secret) trending_topics = api.GetTrendsWoeid(BOSTON_WOEID) for topic in trending_topics: util.safe_print(topic.name)
def search(searchTerm): """ Print recent tweets containing `searchTerm`. To test this function, at the command line run: python twitter_api.py --search=<search term> For example, python twitter_api.py --search=python """ tweets = api.GetSearch(searchTerm) for tweet in tweets: util.safe_print(tweet.GetText())
def attempt_save_timeout_thread(self, threadNum): # initiate leader election within [10, 15) seconds of receiving a moneyTransfer timeout = random.uniform(10, 15) sleep(timeout) self.saveThreadLock.acquire() if self.currentSaveThreadNum != threadNum: self.saveThreadLock.release() return self.saveThreadLock.release() safe_print( 'Starting save attempt after {0} seconds....'.format(timeout)) self.attempt_save() # won't save empty blocks
def trendingTopics(): """ Print the currently trending topics. To test this function, at the command line run: python twitter_api.py -t """ api = twitter.Api() trending_topics = api.GetTrendsWoeid(BOSTON_WOEID) for topic in trending_topics: util.safe_print(topic.name)
def trendingTweets(): """ Print tweets for all the trending topics. To test this function, at the command line run: python twitter_api.py -w """ trending_topics = api.GetTrendsWoeid(PORTLAND_WOEID) for topic in trending_topics: trending_tweets = api.GetSearch(topic.name) for tweet in trending_tweets: util.safe_print(tweet.GetText())
def search(searchTerm): """ Print recent tweets containing `searchTerm`. To test this function, at the command line run: python twitter_api.py --search=<search term> For example, python twitter_api.py --search=python """ api = twitter.Api() tweets = api.GetSearch(searchTerm) for tweet in tweets: util.safe_print(tweet.GetText())
def search(searchTerm): """ Print recent tweets containing `searchTerm`. To test this function, at the command line run: python twitter_api.py --search=<search term> For example, python twitter_api.py --search=python """ api = twitter.Api(consumer_key=key,consumer_secret=secret,access_token_key=access_key,access_token_secret=access_secret) tweets = api.GetSearch(searchTerm) for tweet in tweets: util.safe_print(tweet.GetText())
def trendingTweets(): """ Print tweets for all the trending topics. To test this function, at the command line run: python twitter_api.py -w """ api = twitter.Api(consumer_key=key, consumer_secret=secret, access_token_key=access_key, access_token_secret=access_secret) trending_topics = api.GetTrendsWoeid(BOSTON_WOEID) for tweet in trending_topics: util.safe_print(tweet.GetText())
def userTweets(username): """ Print recent tweets by `username`. You may find the twitter.Api() function GetUserTimeline() helpful. To test this function, at the command line run: python twitter_api.py -u <username> For example, python twitter_api.py -u bostonpython """ user_tweets = api.GetUserTimeline(username) for tweet in user_tweets: util.safe_print(tweet.GetText())
def userTweets(username): """ Print recent tweets by `username`. You may find the twitter.Api() function GetUserTimeline() helpful. To test this function, at the command line run: python twitter_api.py -u <username> For example, python twitter_api.py -u bostonpython """ api = twitter.Api(consumer_key=key,consumer_secret=secret,access_token_key=access_key,access_token_secret=access_secret) user_tweets = api.GetUserTimeline(screen_name=username) for tweet in user_tweets: util.safe_print(tweet.GetText())
def process_recv_msg(self, msg): self.lock.acquire() msg_pid = msg['header']['pid'] msg_type = msg['header']['type'] msg_ballotNum = msg['header']['ballotNum'] safe_print( "Received {0} message from node {1} with ballotNum <{2}, {3}, {4}>" .format(msg_type, msg_pid, msg_ballotNum['num'], msg_ballotNum['pid'], msg_ballotNum['depth'])) if msg_type == 'blockUpdateReq': self.process_block_update_req_msg(msg) elif msg_type == 'blockUpdateRes': self.process_block_update_res_msg(msg) elif msg_ballotNum['depth'] < self.depth: # this means that an outdated node sent you a message; you don't want to respond to it normally # forge a blockUpdateReq message from that node so that you can process it locally and send back a blockUpdateRes if it was an outdated prepare msg if msg_type == 'prepare': sendToConfig = self.globalConfig[ self.__get_server_index_from_pid( self.pid)] # send to yourself block_update_req_msg = create_block_update_req_msg( msg_pid, msg_ballotNum ) # forge message as if it was created by the node self.sock.delayed_send( block_update_req_msg, (sendToConfig['ip_addr'], sendToConfig['port']), msg_pid) elif msg_ballotNum['depth'] > self.depth: # send blockchain request to the node you received a msg from since you'd be out-of-date sendToConfig = self.globalConfig[self.__get_server_index_from_pid( msg_pid)] block_update_req_msg = create_block_update_req_msg( self.pid, self.ballotNum) self.sock.delayed_send( block_update_req_msg, (sendToConfig['ip_addr'], sendToConfig['port']), msg_pid) elif msg_type == 'accept': self.process_accept_msg(msg) elif msg_type == 'ack': self.process_ack_msg(msg) elif msg_type == 'decision': self.process_decision_msg(msg) elif msg_type == 'prepare': self.process_prepare_msg(msg) else: self.lock.release() raise Exception('Incorrect msg format' + str(msg)) self.dumpDisk() self.lock.release()
def trendingTopics(woeid): """twitter apisearch Print the currently trending topics. To test this function, at the command line run: python twitter_api.py -t """ try: trending_topics = api.GetTrendsWoeid(woeid) except twitter.TwitterError: trending_topics = api.GetTrendsCurrent() except twitter.urllib2.URLError: print("Unable to connect to network, giving up.") twitter.sys.exit() for topic in trending_topics: util.safe_print(topic.name)
def trendingTweets(): """ Print tweets for all the trending topics. To test this function, at the command line run: python twitter_api.py -w """ api = twitter.Api() trending_topics = api.GetTrendsWoeid(BOSTON_WOEID) for topic in trending_topics: topic_name = topic.name.encode('ascii', 'ignore') get_tweets = api.GetSearch(topic.name) util.safe_print(topic.name) for tweet in get_tweets: util.safe_print(tweet.GetText())
def search(searchTerm): """ Print recent tweets containing `searchTerm`. To test this function, at the command line run: python twitter_api.py --search=<search term> For example, python twitter_api.py --search=python """ api = twitter.Api(consumer_key=key, consumer_secret=secret, access_token_key=access_key, access_token_secret=access_secret) tweets = api.GetSearch(searchTerm) for tweet in tweets: util.safe_print(tweet.GetText())
def trendingTopics(woeid): """twitter apisearch Print the currently trending topics. To test this function, at the command line run: python raven.py -t """ try: trending_topics = api.GetTrendsWoeid(woeid) except twitter.TwitterError: trending_topics = api.GetTrendsCurrent() except twitter.urllib2.URLError: print ("Unable to connect to network, giving up.") twitter.sys.exit() for topic in trending_topics: util.safe_print(topic.name)
def userTweets(username): """ Print recent tweets by `username`. You may find the twitter.Api() function GetUserTimeline() helpful. To test this function, at the command line run: python twitter_api.py -u <username> For example, python twitter_api.py -u bostonpython """ api = twitter.Api(consumer_key=key, consumer_secret=secret, access_token_key=access_key, access_token_secret=access_secret) user_tweet = api.GetUserTimeline(screen_name=username) for tweet in user_tweet: util.safe_print(tweet.GetText())
def userHomeTimeline(): """ Fetch a collection of the most recent Tweets and retweets posted by the authenticating user and the users they follow. """ try: tweets = api.GetHomeTimeline(count=5) except twitter.TwitterError: print ("User @" + username + " not found") twitter.sys.exit() except twitter.urllib2.URLError: print ("Unable to connect to network, giving up.") twitter.sys.exit() print '' for tweet in tweets: printScreenName(tweet.user.screen_name) util.safe_print(tweet.GetText()) print ''
def userTweets(username): """ Print recent tweets by `username`. You may find the twitter.Api() function GetUserTimeline() helpful. To test this function, at the command line run: python twitter_api.py -u <username> For example, python twitter_api.py -u bostonpython """ try: tweets = api.GetUserTimeline(screen_name=username) except twitter.TwitterError: print("User @" + username + " not found") twitter.sys.exit() except twitter.urllib2.URLError: print("Unable to connect to network, giving up.") twitter.sys.exit() for tweet in tweets: util.safe_print(tweet.GetText())
def search(searchTerm): """ Print recent tweets containing `searchTerm`. To test this function, at the command line run: python twitter_api.py --search=<search term> For example, python twitter_api.py --search=python """ try: tweets = api.GetSearch(searchTerm) except twitter.urllib2.URLError: print("Unable to connect to network, giving up.") twitter.sys.exit() if len(tweets) > 15: tweets = tweets[:15] for tweet in tweets: print '@' + tweet.user.screen_name + ': ', util.safe_print(tweet.GetText())
def userTweets(username): """ Print recent tweets by `username`. You may find the twitter.Api() function GetUserTimeline() helpful. To test this function, at the command line run: python raven.py -u <username> For example, python raven.py -u bostonpython """ try: tweets = api.GetUserTimeline(screen_name=username, count=5) except twitter.TwitterError: print ("User @" + username + " not found") twitter.sys.exit() except twitter.urllib2.URLError: print ("Unable to connect to network, giving up.") twitter.sys.exit() for tweet in tweets: util.safe_print(tweet.GetText())
def trendingTweets(woeid): """ Print tweets for all the trending topics. To test this function, at the command line run: python twitter_api.py -w """ try: trending_topics = api.GetTrendsWoeid(woeid) except twitter.TwitterError: trending_topics = api.GetTrendsCurrent() except twitter.urllib2.URLError: print("Unable to connect to network, giving up.") twitter.sys.exit() for topic in trending_topics: print "**", topic.name esc_topic_name = twitter.urllib2.quote(topic.name.encode('utf8')) tweets = api.GetSearch(esc_topic_name) for tweet in tweets[:5]: print '@' + tweet.user.screen_name + ': ', util.safe_print(tweet.GetText()) print '\n'
def find(searchTerm): """ Print recent tweets containing `searchTerm`. To test this function, at the command line run: python raven.py --search=<search term> For example, python raven.py --search=python """ try: tweets = api.GetSearch(searchTerm) except twitter.urllib2.URLError: print ("Unable to connect to network, giving up.") twitter.sys.exit() if len(tweets) > 10: tweets = tweets[:10] print '' for tweet in tweets: printScreenName(tweet.user.screen_name) util.safe_print(tweet.GetText()) print ''
def trendingTweets(woeid): """ Print tweets for all the trending topics. To test this function, at the command line run: python raven.py -w """ try: trending_topics = api.GetTrendsWoeid(woeid) except twitter.TwitterError: trending_topics = api.GetTrendsCurrent() except twitter.urllib2.URLError: print ("Unable to connect to network, giving up.") twitter.sys.exit() print '' for topic in trending_topics: print colour.yellow + "**",topic.name + colour.end esc_topic_name = twitter.urllib2.quote(topic.name.encode('utf8')) tweets = api.GetSearch(esc_topic_name) for tweet in tweets[:5]: printScreenName(tweet.user.screen_name) util.safe_print(tweet.GetText()) print ''
def trendingTweets(): """ Print tweets for all the trending topics. To test this function, at the command line run: python twitter_api.py -w """ api = twitter.Api(consumer_key=key,consumer_secret=secret,access_token_key=access_key,access_token_secret=access_secret) trending_topics = api.GetTrendsCurrent() for topic in trending_topics: util.safe_print(topic.name) tweets = api.GetSearch(topic.name, count=2) for tweet in tweets[:2]: util.safe_print(tweet.GetText()) util.safe_print('-------------------------------------------')
while True: data, addr = serverSocket.recvfrom(1024) msg = json.loads(data) threading.Thread(target=paxosManager.process_recv_msg, args=(msg,)).start() t = threading.Thread(target=listenRequests) t.daemon = True t.start() while True: cmd = raw_input("") if 'moneyTransfer' in cmd: moneyTransferStr, creditNode, costStr = cmd.split(',') transaction = { 'debitNode': localConfig['id'], 'creditNode': creditNode, 'cost': int(costStr) } if transaction['creditNode'] not in serverIdList: safe_print('Invalid creditNode') else: try: paxosManager.add_transaction(transaction) except Exception as e: safe_print(e) elif 'networkDown' in cmd: cmdName, pid = cmd.split(',') if pid == localConfig['id']: safe_print('You cannot take down a network communication between yourself') else: connectGraph.network_down(pid) elif 'networkUp' in cmd: cmdName, pid = cmd.split(',') if pid == localConfig['id']: safe_print('You cannot take down a network communication between yourself')
def printtweet(self, tweetlist): for tweet in tweetlist: print tweet.user.id, tweet.user.screen_name, safe_print(tweet.text), tweet.retweeted, tweet.retweet_count, \ tweet.user.location, tweet.created_at
def search(searchTerm): api = twitter.Api(consumer_key=key,consumer_secret=secret,access_token_key=access_key,access_token_secret=access_secret) tweets = api.GetSearch(searchTerm) for tweet in tweets: util.safe_print(tweet.GetText())
def program_segment_and_membership_rule(server, cookie, rule, tenant, port=const.BCF_CONTROLLER_PORT): if rule.segment not in const.IVS_INTERNAL_PORT_DIC: return existing_segments = RestLib.get_os_mgmt_segments( server, cookie, tenant, port) if rule.segment not in existing_segments: with open(const.LOG_FILE, "a") as log_file: msg = (r'''Warning: BCF controller does not have tenant ''' '''%(tenant)s segment %(segment)s\n''' % {'tenant': tenant, 'segment': rule.segment}) safe_print(msg) log_file.write(msg) return segment_url = (r'''applications/bcf/tenant[name="%(tenant)s"]/segment''' % {'tenant': tenant}) segment_data = {"name": rule.segment} safe_print("Configuring BCF Segment: Tenant %s, Segment %s\n" % (tenant, rule.segment)) try: ret = RestLib.post(cookie, segment_url, server, port, json.dumps(segment_data)) except Exception: ret = RestLib.patch(cookie, segment_url, server, port, json.dumps(segment_data)) if ret[0] != 204: if (ret[0] != 409 or const.ELEMENT_EXISTS not in ret[2]): raise Exception(ret) if rule.br_vlan: vlan = int(rule.br_vlan) else: vlan = -1 intf_rule_url = (r'''applications/bcf/tenant[name="%(tenant)s"]/''' '''segment[name="%(segment)s"]/''' '''switch-port-membership-rule''' % {'tenant': tenant, 'segment': rule.segment}) rule_data = {"interface": const.ANY, "switch": const.ANY, "vlan": vlan} safe_print("Configuring BCF Segment rule: Tenant %s, Segment " "%s Rule: member switch any interface any vlan %d\n" % (tenant, rule.segment, vlan)) try: ret = RestLib.post(cookie, intf_rule_url, server, port, json.dumps(rule_data)) except Exception: ret = RestLib.patch(cookie, intf_rule_url, server, port, json.dumps(rule_data)) if ret[0] != 204: if (ret[0] != 409 or const.ELEMENT_EXISTS not in ret[2]): raise Exception(ret) pg_rule_url = (r'''applications/bcf/tenant[name="%(tenant)s"]/''' '''segment[name="%(segment)s"]/''' '''interface-group-membership-rule''' % {'tenant': tenant, 'segment': rule.segment}) rule_data = {"interface-group": const.ANY, "vlan": vlan} safe_print("Configuring BCF Segment rule: Tenant %s, " "Segment %s Rule: member interface-group any vlan %d\n" % (tenant, rule.segment, vlan)) try: ret = RestLib.post(cookie, pg_rule_url, server, port, json.dumps(rule_data)) except Exception: ret = RestLib.patch(cookie, pg_rule_url, server, port, json.dumps(rule_data)) if ret[0] != 204: if (ret[0] != 409 or const.ELEMENT_EXISTS not in ret[2]): raise Exception(ret) specific_rule_url = (r'''applications/bcf/tenant[name="%(tenant)s"]/''' '''segment[name="%(segment)s"]/''' '''switch-port-membership-rule''' % {'tenant': tenant, 'segment': rule.segment}) rule_data = {"interface": rule.internal_port, "switch": const.ANY, "vlan": -1} safe_print("Configuring BCF Segment rule: Tenant %s, Segment %s Rule: " "member switch any interface %s vlan untagged\n" % (tenant, rule.segment, rule.internal_port)) try: ret = RestLib.post(cookie, specific_rule_url, server, port, json.dumps(rule_data)) except Exception: ret = RestLib.patch(cookie, specific_rule_url, server, port, json.dumps(rule_data)) if ret[0] != 204: if (ret[0] != 409 or const.ELEMENT_EXISTS not in ret[2]): raise Exception(ret)
def program_segment_and_membership_rule(server, cookie, rule, tenant, port=const.BCF_CONTROLLER_PORT): existing_segments = RestLib.get_os_mgmt_segments( server, cookie, tenant, port) if rule.segment not in existing_segments: with open(const.LOG_FILE, "a") as log_file: msg = (r'''Warning: BCF controller does not have tenant ''' '''%(tenant)s segment %(segment)s\n''' % {'tenant': tenant, 'segment': rule.segment}) safe_print(msg) log_file.write(msg) return segment_url = (r'''applications/bcf/tenant[name="%(tenant)s"]/''' '''segment[name="%(segment)s"]''' % {'tenant': tenant, 'segment': rule.segment}) segment_data = {"name": rule.segment} safe_print("Configuring BCF Segment: Tenant %s, Segment %s\n" % (tenant, rule.segment)) ret = RestLib.post(cookie, segment_url, server, port, json.dumps(segment_data)) if ret[0] != 204: raise Exception(ret) if rule.br_vlan: vlan = int(rule.br_vlan) else: vlan = -1 intf_rule_url = (r'''applications/bcf/tenant[name="%(tenant)s"]/''' '''segment[name="%(segment)s"]/''' '''switch-port-membership-rule[interface="''' '''%(interface)s"][switch="%(switch)s"]''' '''[vlan=%(vlan)d]''' % {'tenant': tenant, 'segment': rule.segment, 'interface': const.ANY, 'switch': const.ANY, 'vlan': vlan}) rule_data = {"interface": const.ANY, "switch": const.ANY, "vlan": vlan} safe_print("Configuring BCF Segment rule: Tenant %s, Segment " "%s Rule: member switch any interface any vlan %d\n" % (tenant, rule.segment, vlan)) ret = RestLib.post(cookie, intf_rule_url, server, port, json.dumps(rule_data)) if ret[0] != 204: raise Exception(ret) pg_rule_url = (r'''applications/bcf/tenant[name="%(tenant)s"]/''' '''segment[name="%(segment)s"]/''' '''port-group-membership-rule[port-group="%(pg)s"]''' '''[vlan=%(vlan)d]''' % {'tenant': tenant, 'segment': rule.segment, 'pg': const.ANY, 'vlan': vlan}) rule_data = {"port-group": const.ANY, "vlan": vlan} safe_print("Configuring BCF Segment rule: Tenant %s, " "Segment %s Rule: member port-group any vlan %d\n" % (tenant, rule.segment, vlan)) ret = RestLib.post(cookie, pg_rule_url, server, port, json.dumps(rule_data)) if ret[0] != 204: raise Exception(ret) specific_rule_url = (r'''applications/bcf/tenant[name="%(tenant)s"]/''' '''segment[name="%(segment)s"]/''' '''switch-port-membership-rule''' '''[interface="%(interface)s"]''' '''[switch="%(switch)s"][vlan=%(vlan)d]''' % {'tenant': tenant, 'segment': rule.segment, 'interface': rule.internal_port, 'switch': const.ANY, 'vlan': -1}) rule_data = {"interface": rule.internal_port, "switch": const.ANY, "vlan": -1} safe_print("Configuring BCF Segment rule: Tenant %s, Segment %s Rule: " "member switch any interface %s vlan untagged\n" % (tenant, rule.segment, rule.internal_port)) ret = RestLib.post(cookie, specific_rule_url, server, port, json.dumps(rule_data)) if ret[0] != 204: raise Exception(ret)
def program_segment_and_membership_rule(server, cookie, rule, tenant, port=const.BCF_CONTROLLER_PORT): use_port_group = RestLib.use_port_group(server, cookie) pg_key = 'interface' if use_port_group: pg_key = 'port' if rule.segment not in const.IVS_INTERNAL_PORT_DIC: return existing_segments = RestLib.get_os_mgmt_segments( server, cookie, tenant, port) if rule.segment not in existing_segments: with open(const.LOG_FILE, "a") as log_file: msg = (r'''Warning: BCF controller does not have tenant ''' '''%(tenant)s segment %(segment)s\n''' % { 'tenant': tenant, 'segment': rule.segment }) safe_print(msg) log_file.write(msg) return segment_url = ( r'''applications/bcf/tenant[name="%(tenant)s"]/segment''' % { 'tenant': tenant }) segment_data = {"name": rule.segment} safe_print("Configuring BCF Segment: Tenant %s, Segment %s\n" % (tenant, rule.segment)) try: ret = RestLib.post(cookie, segment_url, server, port, json.dumps(segment_data)) except Exception: ret = RestLib.patch(cookie, segment_url, server, port, json.dumps(segment_data)) if ret[0] != 204: if (ret[0] != 409 or const.ELEMENT_EXISTS not in ret[2]): raise Exception(ret) if rule.br_vlan: vlan = int(rule.br_vlan) else: vlan = -1 intf_rule_url = (r'''applications/bcf/tenant[name="%(tenant)s"]/''' '''segment[name="%(segment)s"]/''' '''switch-port-membership-rule''' % { 'tenant': tenant, 'segment': rule.segment }) rule_data = {"interface": const.ANY, "switch": const.ANY, "vlan": vlan} safe_print("Configuring BCF Segment rule: Tenant %s, Segment " "%s Rule: member switch any interface any vlan %d\n" % (tenant, rule.segment, vlan)) try: ret = RestLib.post(cookie, intf_rule_url, server, port, json.dumps(rule_data)) except Exception: ret = RestLib.patch(cookie, intf_rule_url, server, port, json.dumps(rule_data)) if ret[0] != 204: if (ret[0] != 409 or const.ELEMENT_EXISTS not in ret[2]): raise Exception(ret) pg_rule_url = (r'''applications/bcf/tenant[name="%(tenant)s"]/''' '''segment[name="%(segment)s"]/''' '''%(pg_key)s-group-membership-rule''' % { 'tenant': tenant, 'pg_key': pg_key, 'segment': rule.segment }) rule_data = {"%s-group" % pg_key: const.ANY, "vlan": vlan} safe_print("Configuring BCF Segment rule: Tenant %s, " "Segment %s Rule: member %s-group any vlan %d\n" % (tenant, rule.segment, pg_key, vlan)) try: ret = RestLib.post(cookie, pg_rule_url, server, port, json.dumps(rule_data)) except Exception: ret = RestLib.patch(cookie, pg_rule_url, server, port, json.dumps(rule_data)) if ret[0] != 204: if (ret[0] != 409 or const.ELEMENT_EXISTS not in ret[2]): raise Exception(ret) specific_rule_url = (r'''applications/bcf/tenant[name="%(tenant)s"]/''' '''segment[name="%(segment)s"]/''' '''switch-port-membership-rule''' % { 'tenant': tenant, 'segment': rule.segment }) rule_data = { "interface": rule.internal_port, "switch": const.ANY, "vlan": -1 } safe_print("Configuring BCF Segment rule: Tenant %s, Segment %s Rule: " "member switch any interface %s vlan untagged\n" % (tenant, rule.segment, rule.internal_port)) try: ret = RestLib.post(cookie, specific_rule_url, server, port, json.dumps(rule_data)) except Exception: ret = RestLib.patch(cookie, specific_rule_url, server, port, json.dumps(rule_data)) if ret[0] != 204: if (ret[0] != 409 or const.ELEMENT_EXISTS not in ret[2]): raise Exception(ret)