Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix issue(#823) mysql-instance #860

Merged

Conversation

professorabhay
Copy link
Contributor

Problem

Need support for Linode MySQL instance

Solution

Create a sql.go file to collect MySQL Instances.

Changes Made

1- created a dir and file -> /sql/sql.go
2- modify instance.go to determine cost

Reviewers

@ShubhamPalriwala @jakepage91

To display cost as per usage.
@professorabhay
Copy link
Contributor Author

Hey @ShubhamPalriwala, I tried to add and determine cost of client of SQL as per the metadata from the link you provided.
Test it and provide me the review on it !!
Thank-you 🙂

@ShubhamPalriwala
Copy link
Collaborator

Hey @professorabhay, you missed calling the resource, please take a look at this amazing video by @jakepage91: https://www.youtube.com/watch?v=Vn5uc2elcVg

This adds a similar feature to yours and you will know how to call it and possibly add tests if possible!

imported sql.go
//make call to the `GetSQLInstances` function of /sql/sql.go
@professorabhay
Copy link
Contributor Author

Hey @ShubhamPalriwala, I checked that video and its really helpful. And did the needed changes.
Can you try running the test again?

Copy link
Collaborator

@ShubhamPalriwala ShubhamPalriwala left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The PR looks amazing now! 🚀 Thanks a ton!

One small nit from my end, if you wanna make the code even cleaner:
I took a deeper look, and the the pricing for:

  • Dedicated x GB is of the form:
    • 1 Node: y
    • 3 Node: 3*Y
  • Shared CPU x GB is of the form:
    • 1 Node: z
    • 3 Node: z*(2.333)

So you can prolly just store the single node price and then see if the API returns 1 node or 3 node and then do cost calculation accordingly in a function! This reduces the hardcoding of values and helps in easier updates to the cost if needed.

Let me know if you want to take this up else I can help you out with this! 🥳

@professorabhay
Copy link
Contributor Author

Hai @ShubhamPalriwala, I can modify the code accordingly.

@professorabhay
Copy link
Contributor Author

Hey @ShubhamPalriwala, I tried to modify the file.
You can look at here before I commit - https://github.com/professorabhay/Temp-files/blob/main/Komiser/sql.go
I invited you as a collaborator on that private repo.

providers/linode/sql/sql.go Outdated Show resolved Hide resolved
@mlabouardy mlabouardy added this to the v3.0.20 milestone Jun 19, 2023
@professorabhay
Copy link
Contributor Author

Hey @mlabouardy, I make the changes.
Kindly take a look to them

@ShubhamPalriwala
Copy link
Collaborator

Hey @ShubhamPalriwala, I tried to modify the file. You can look at here before I commit - https://github.com/professorabhay/Temp-files/blob/main/Komiser/sql.go I invited you as a collaborator on that private repo.

Hey I just took a look and this approach:

	if strings.HasPrefix(instanceType, "Dedicated") {
		cost, ok := dedicatedCPUCosts[instanceType]
		if !ok {
			return 0, false
		}

		// Adjust cost for 3 Node instances
		if nodeCount == 3 {
			cost *= 3
		}
	} else if strings.HasPrefix(instanceType, "Shared") {
		cost, ok := sharedCPUCosts[instanceType]
		if !ok {
			return 0, false
		}

		// Adjust cost for 3 Node instances
		if nodeCount == 3 {
			cost *= 2.333
		}
	} else {
		return 0, false
	}

lgtm!

So you can push the changes once tested!

@professorabhay
Copy link
Contributor Author

Sure @ShubhamPalriwala, I will do it in some time.

@professorabhay
Copy link
Contributor Author

Hey @ShubhamPalriwala and @mlabouardy, Kindly take a look to the new commit !! 🙂

Copy link
Collaborator

@ShubhamPalriwala ShubhamPalriwala left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks a lot @professorabhay, the PR looks amazing to me now! Just ran the CI, if that passes then over to you @mlabouardy 🚀

@professorabhay
Copy link
Contributor Author

Hey @ShubhamPalriwala, Is all the test pass or not??

@ShubhamPalriwala
Copy link
Collaborator

Hey @professorabhay, I see the build failing in the CI! I'm taking a look rn

@ShubhamPalriwala
Copy link
Collaborator

Hey, did you test this locally? I can also reproduce the build error locally from below:

// Instances fetches SQL instances from the provider and returns them as resources.
func Instances(ctx context.Context, client providers.ProviderClient) ([]models.Resource, error) {
	resources := make([]models.Resource, 0)

	instances, err := client.SQLClient.GetInstances(ctx)
	if err != nil {
		return resources, err
	}
    ...
}

A good start to debugging this is to use the correct client ie client.LinodeClient instead of client.SQLClient which does not exist! I would recommend you to test it once locally once you have updated the client and then made necessary changes. If you need any help in the meantime, do ping here or even hop on Discord

@professorabhay
Copy link
Contributor Author

Sure, I am looking into it.
Will update you ASAP !!

@mlabouardy
Copy link
Collaborator

Sure, I am looking into it. Will update you ASAP !!

Hey @professorabhay, let me know if you need a hand with this :) so we can merge it this week!

@professorabhay
Copy link
Contributor Author

Hello @ShubhamPalriwala, It seems that the NodeCount value is not available in the Linode cloud's API or in the linodego.Instance struct. How, Can I resolve it ?

@professorabhay
Copy link
Contributor Author

image

@ShubhamPalriwala
Copy link
Collaborator

Hey @professorabhay, try exploring the metadata you receive in the instance struct and see if the API provides the number of nodes anywhere? If then we can write a method to parse it accordingly, else we would then need to find a different identifier for the number of nodes for cost calculation! Let me know if you tried and aren't able to at all, then I'll try to go hands-on with it

@professorabhay
Copy link
Contributor Author

Yes @ShubhamPalriwala , I took a look into the Linode API and there is nothing like node count and similar like this. Instead of count there is some sort of ID's.
Can you please take a look once and let me know if you find anything related this?
Then will modify the code accordingly!!

@ShubhamPalriwala
Copy link
Collaborator

Hey @professorabhay, I just went through your PR locally. A couple of pointers:
So the goal here was and is to add support for the MySQL Instances right? so intead of trying to generalise the database support, have it atomically only.

  • Rename the file to: linode/sql/mysql.go
  • Try using the client.LinodeClient.ListMySQLDatabases() method instead for direct querying of the MySQL instances
  • And then go through the API response here (this is the same response you would get from the go-sdk): https://www.linode.com/docs/api/databases/#managed-mysql-databases-list__response-samples to see how you can utilise some properties for cost calculation (hint: look at the variables you have in the cost calculation and see what property can get you that and if something is missing, try to co-relate it and create a mapping in our codebase)

You have already did most of the hardwork so the above pointers should help you, if not, lets hop on Discord sometime and get things going 🏃🏼

@professorabhay
Copy link
Contributor Author

Hey @ShubhamPalriwala, I tried it out as you said.
Please take a look to the latest mysql.go commit which passes the test - professorabhay#5
I just need to modify the cost calculation logic for 3 nodes. But before I do that I just want you take a look to the main functions of the code.

@ShubhamPalriwala
Copy link
Collaborator

Hey @professorabhay, I took a quick glance and here are my suggestions:

  • We should not add the field NodeCount to the resource type since not every resource would have it and then it would leave to allocation of extra memory per resource, you can rather store it in the metadata like in a tag if you want
  • Let's not modify the instances.go and keep it as it is, this is because it is perfectly solving the use case it was built for
  • And here as well, as I suggested, try using the client.LinodeClient.GetMySQLDatabase() method as you did in the method below it. Try to use this method itself for all the instance fetching that you need here for this feature
  • Fetching all the "instances" would take up more wait as well as processing time hence since we already have a method from their sdk to fetch the MySQL DBs directly, let's utilise that itself

@professorabhay
Copy link
Contributor Author

Hey @ShubhamPalriwala, I'll keep all the point in mind and come with an update soon.
For instance.go there is nothing modified in the recent commit. I reverted all changes to the original file.

@professorabhay
Copy link
Contributor Author

Hey @ShubhamPalriwala, Please take a look to latest commit here - professorabhay#5

@ShubhamPalriwala
Copy link
Collaborator

Hey @professorabhay, I'm unable to see the relevant commit there, I would recommend once you test it locally, please push it here on this PR branch and I'd be happy to review it

@professorabhay
Copy link
Contributor Author

Hey @ShubhamPalriwala, Take a look

@mlabouardy mlabouardy merged commit 011211e into tailwarden:develop Jul 6, 2023
2 checks passed
@professorabhay professorabhay deleted the fix-issue(#823)-MySQL-Instance branch July 8, 2023 08:32
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants