Skip to content

Commit

Permalink
Correctly look up tenancy name
Browse files Browse the repository at this point in the history
  • Loading branch information
intjonathan-forcepoint committed Jan 29, 2024
1 parent 3cd1b13 commit 8c6ad52
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 22 deletions.
53 changes: 37 additions & 16 deletions python/anycostoci.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ def __create_or_verify_bdid_folder(start_date: datetime, output_dir: str, drop_i
first_of_the_month = start_date.replace(day=1)
# Calculate the last date of that month
last_date_of_the_month = calendar.monthrange(start_date.year, start_date.month)[1]
# Make a datetime for that last date.
# Make a datetime for that last date.
last_of_the_month = start_date.replace(day=last_date_of_the_month)
# Add 1 day to get the first of the next month
first_of_next_month = last_of_the_month + timedelta(days=1)
Expand Down Expand Up @@ -60,17 +60,24 @@ def __tenancy_name_lookup(tenancy_id: str, oci_config) -> str:
if tenancy_name != None:
# print(f"Found tenancy name {tenancy_name}")
return tenancy_name

try:
print(f"Tenant ID {tenancy_id} was uncached, making API call...")

oci.config.validate_config(oci_config)
iam = oci.identity.IdentityClient(oci_config)
# print(f"Tenant ID {tenancy_id} was uncached, making API call...")
get_tenancy_response = iam.get_tenancy(tenancy_id)
org = oci.tenant_manager_control_plane.OrganizationClient(oci_config)

# Get the organization OCID of the client
get_org_response = org.list_organizations(oci_config['tenancy'])
org_ocid = get_org_response.data.items[0].id

# Get the tenancy given the org OCID
get_tenancy_response = org.get_organization_tenancy(org_ocid, tenancy_id)
tenancy_name = get_tenancy_response.data.name
tenancy_id_cache[tenancy_id] = tenancy_name
# print(f"Tenant ID {tenancy_id} lookup success, name was {tenancy_name}")
print(f"Tenant ID {tenancy_id} lookup success, name was {tenancy_name}")
except oci.exceptions.ServiceError:
# print(f"Tenant ID {tenancy_id} lookup failed, setting empty")
print(f"Tenant ID {tenancy_id} lookup failed, setting empty")
tenancy_name = ""
tenancy_id_cache[tenancy_id] = tenancy_name

Expand Down Expand Up @@ -117,7 +124,7 @@ def download_oci_cost_files(lookback_months: int, oci_config, output_dir = '/tmp

def build_anycost_drop_from_oci_files(lookback_months: int,
oci_config,
oci_cost_files_dir = '/tmp/',
oci_cost_files_dir = '/tmp/',
output_dir = '/tmp/anycost_drop/') -> slice:
"""Take a directory of gzipped OCI cost reports and build an AnyCost drop out of them.
Expand All @@ -140,9 +147,9 @@ def build_anycost_drop_from_oci_files(lookback_months: int,
drop_paths = set()

for root, dirs, cost_files in os.walk(oci_cost_files_dir):
# It would be swell if this yielded the files in order.
# It would be swell if this yielded the files in order.
# The filenames are ordered numbers and we could display progress
for cost_file in cost_files:
for cost_file in cost_files:
if not re.match(".*\.csv\.gz$", cost_file):
continue

Expand All @@ -152,7 +159,7 @@ def build_anycost_drop_from_oci_files(lookback_months: int,
oci_cost = pandas.read_csv(f)
except pandas.errors.EmptyDataError:
print(f"No rows read from file {root}/{cost_file}")

# Start building the CBF formatted frame
cbf_frame = pandas.DataFrame([])

Expand Down Expand Up @@ -188,9 +195,23 @@ def build_anycost_drop_from_oci_files(lookback_months: int,
tag_column = "resource/tag:" + oci_tag_key_cleaned
cbf_frame.insert(len(cbf_frame.columns), tag_column, oci_cost.loc[:, c])

# Synthesized tag for account name
# Synthesized Tags
# Tenancy Name
cbf_frame.insert(len(cbf_frame.columns), 'resource/tag:oci_tenancy_name', oci_cost.loc[:, 'lineItem/tenantId'])
cbf_frame['resource/tag:oci_tenancy_name'] = cbf_frame['resource/tag:oci_tenancy_name'].map(lambda t:__tenancy_name_lookup(t, oci_config))
# Compartment Name
cbf_frame.insert(len(cbf_frame.columns),
"resource/tag:oci_compartment_name",
oci_cost.loc[:, "product/compartmentName"])
# Compartment ID
cbf_frame.insert(len(cbf_frame.columns),
"resource/tag:oci_compartment_id",
oci_cost.loc[:, "product/compartmentId"])
# Availability Domain
cbf_frame.insert(len(cbf_frame.columns),
"resource/tag:oci_availability_domain",
oci_cost.loc[:, "product/availabilityDomain"])


# This section prunes the CBF frames to contain only rows with
# usage_start dates within the BDID boundary.
Expand All @@ -200,7 +221,7 @@ def build_anycost_drop_from_oci_files(lookback_months: int,
cbf_frame['time/usage_end'] = pandas.to_datetime(cbf_frame['time/usage_end'], cache=True)

# Create new date-only timestamp columns so we can look at those for pruning
# note the .dt property refers to the datetime object inside the column
# note the .dt property refers to the datetime object inside the column
cbf_frame['time/usage_start_date'] = cbf_frame['time/usage_start'].dt.date
cbf_frame['time/usage_end_date'] = cbf_frame['time/usage_end'].dt.date

Expand All @@ -209,10 +230,10 @@ def build_anycost_drop_from_oci_files(lookback_months: int,
# every row to see whether it belongs within the window.
if start_date: # conditional here since @start_date is inside the string
cbf_frame.query('`time/usage_start_date` >= @start_date', inplace=True)

if end_date:
cbf_frame.query('`time/usage_start_date` <= @end_date', inplace=True)

# Finally, let's drop the _date columns since they don't belong
# in the output.
cbf_frame.drop(columns=['time/usage_start_date', 'time/usage_end_date'], inplace=True)
Expand All @@ -228,7 +249,7 @@ def build_anycost_drop_from_oci_files(lookback_months: int,
print(f"No rows remaining after date window prune in file {cost_file}")

# Emit manifest pointing at our current drop.
# There should only be 1, despite the for statement.
# There should only be 1, despite the for statement.
for d in drop_paths:
manifest = {
"version": "1.0.0",
Expand Down
9 changes: 4 additions & 5 deletions python/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,7 @@

print(f"Args given: {args}")

# temp_dir = tempfile.TemporaryDirectory(dir=args.temp_dir)
temp_dir = '/tmp/'
temp_dir = args.temp_dir

# Filesystem sanity check
try:
Expand All @@ -58,14 +57,14 @@
print(f"OCI Config: {oci_config}")

downloaded_reports = anycostoci.download_oci_cost_files(
args.lookback_months,
args.lookback_months,
oci_config = oci_config,
output_dir = oci_write_dir)

output_paths = anycostoci.build_anycost_drop_from_oci_files(
args.lookback_months,
args.lookback_months,
oci_config,
oci_cost_files_dir = oci_write_dir,
oci_cost_files_dir = oci_write_dir,
output_dir = anycost_drop_dir
)

Expand Down
3 changes: 2 additions & 1 deletion python/lambda.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ def anycost(event, context):
)

output_drops = anycostoci.build_anycost_drop_from_oci_files(
lookback_months = lookback_months,
lookback_months,
oci_config,
oci_cost_files_dir = oci_write_dir,
output_dir = anycost_drop_dir,
)
Expand Down

0 comments on commit 8c6ad52

Please sign in to comment.