mgmonteleone / python-atlasapi

python package for MongoDB Atlas Cloud provider
Apache License 2.0
21 stars 22 forks source link

Issues with modify_cluster_instance_size (datetime in not JSON serializable) #182

Closed matiasblancor closed 1 year ago

matiasblancor commented 1 year ago

Hi, I am writen a script that changes our Mogo atlas cluster instance size at night, and then changes it again during the day, but when I want to update the instance size this error is logged

TypeError: Object of type datetime is not JSON serializable

this is my code

from atlasapi.atlas import Atlas
from atlasapi.clusters import InstanceSizeName

atlas_instance_size = {
      "M2": {
          "scale_down": InstanceSizeName.M0,
          "scale_default": InstanceSizeName.M2
      },
      "M5": {
          "scale_down": InstanceSizeName.M2,
          "scale_default": InstanceSizeName.M5
      },
      "M10": {
          "scale_down": InstanceSizeName.M5,
          "scale_default": InstanceSizeName.M10
      },
      "M20": {
          "scale_down": InstanceSizeName.M10,
          "scale_default": InstanceSizeName.M20
      },
      "M30": {
          "scale_down": InstanceSizeName.M20,
          "scale_default": InstanceSizeName.M30
      },
      "M40": {
          "scale_down": InstanceSizeName.M30,
          "scale_default": InstanceSizeName.M40
      },
      "M50": {
          "scale_down": InstanceSizeName.M40,
          "scale_default": InstanceSizeName.M50
      }
  }

public_key="xxxxxxx"
private_key="xxxxxxx"
group_id="xxxxx"
cluster_name = "Cluster0"
default_instance_size = "M5"
atlas = Atlas(public_key, private_key, group_id)

out = atlas.Clusters.modify_cluster_instance_size(cluster=cluster_name, new_cluster_size=atlas_instance_size[default_instance_size]['scale_default'])

if someone knows what I'm doing wrong 🙏🏻


python version: 3.10.0 atlasapi version: 2.0.8

matiasblancor commented 1 year ago

hi there, I found that M0, M2 and M5 clusters can't be updated by public API, so I update my test cluster to M10 to scale to M20, but I things that are a bug because I now use this code

 cluster = atlas.Clusters.get_single_cluster_as_obj(cluster_name).as_modify_dict()
# need to delete de create_date because this gives me the mentioned error
del cluster['create_date']
# need to delete these keys because there are None and the API response with "Bad Request"
del cluster['providerSettings']['volumeType']
del cluster['providerSettings']['encryptEBSVolume']
del cluster['providerSettings']['diskIOPS']

I read on the documentation that ".as_modify_dict()" left the dict ready to be used on modify functions, but the None values broke the execution.