From 526cd68586d273b4fd10e28dfcb4574f483e8903 Mon Sep 17 00:00:00 2001 From: Pascal Fischer Date: Thu, 8 Feb 2024 17:49:31 +0100 Subject: [PATCH] add oracle cloud --- aws.go | 8 ++++---- detect.go | 1 + oracle.go | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 oracle.go diff --git a/aws.go b/aws.go index 2c21387..52af51a 100644 --- a/aws.go +++ b/aws.go @@ -9,11 +9,11 @@ func detectAWS() string { v2ResultChan := make(chan bool, 1) go func() { - v1ResultChan <- detectIDMSv1() + v1ResultChan <- detectAWSIDMSv1() }() go func() { - v2ResultChan <- detectIDMSv2() + v2ResultChan <- detectAWSIDMSv2() }() v1Result, v2Result := <-v1ResultChan, <-v2ResultChan @@ -24,7 +24,7 @@ func detectAWS() string { return "" } -func detectIDMSv1() bool { +func detectAWSIDMSv1() bool { resp, err := hc.Get("http://169.254.169.254/latest/") if err != nil { return false @@ -33,7 +33,7 @@ func detectIDMSv1() bool { return resp.StatusCode == http.StatusOK } -func detectIDMSv2() bool { +func detectAWSIDMSv2() bool { req, err := http.NewRequest("PUT", "http://169.254.169.254/latest/api/token", nil) if err != nil { return false diff --git a/detect.go b/detect.go index a9f9844..bfe5738 100644 --- a/detect.go +++ b/detect.go @@ -19,6 +19,7 @@ func Detect() string { detectAzure, detectDigitalOcean, detectGCE, + detectOracle, detectOpenStack, detectSoftlayer, detectVultr, diff --git a/oracle.go b/oracle.go new file mode 100644 index 0000000..8613806 --- /dev/null +++ b/oracle.go @@ -0,0 +1,53 @@ +package libdetectcloud + +import "net/http" + +func detectOracle() string { + v1ResultChan := make(chan bool, 1) + v2ResultChan := make(chan bool, 1) + + go func() { + v1ResultChan <- detectOracleIDMSv1() + }() + + go func() { + v2ResultChan <- detectOracleIDMSv2() + }() + + v1Result, v2Result := <-v1ResultChan, <-v2ResultChan + + if v1Result || v2Result { + return "Oracle" + } + return "" +} + +func detectOracleIDMSv1() bool { + req, err := http.NewRequest("GET", "http://169.254.169.254/opc/v1/instance/", nil) + if err != nil { + return false + } + req.Header.Add("Authorization", "Bearer Oracle") + + resp, err := hc.Do(req) + if err != nil { + return false + } + defer resp.Body.Close() + return resp.StatusCode == http.StatusOK +} + +func detectOracleIDMSv2() bool { + req, err := http.NewRequest("GET", "http://169.254.169.254/opc/v2/instance/", nil) + if err != nil { + return false + } + req.Header.Add("Authorization", "Bearer Oracle") + + resp, err := hc.Do(req) + if err != nil { + return false + } + defer resp.Body.Close() + return resp.StatusCode == http.StatusOK +}