c, clerr := objectstorage.NewObjectStorageClientWithConfigurationProvider(common.NewRawConfigurationProvider(
"ocid1.tenancy.oc1..aaaaaaa5jo3pz1alm1o45rzx1ucaab4njxbwaqqbc7ld3l6biayjaert5la",
"ocid1.user.oc1..aaaaaaaauax5bo2gg3az46h53467u57ue86rk9h2wax8w7zzamxgwvsi34ja",
"ap-seoul-1",
"98:bc:6b:13:c1:64:ds:8b:9c:15:11:d2:8d:e5:92:db",
))
I'm trying to use oracle object storage, I checked the official manual, but there is something I don't understand. As above, I need the privateKey, and pricateKeyPassphrase arguments, but I don't know where to get them. Is there a detailed explanation or example?
What i want, is to upload a file to storage.
Where can I go to the page in the oracle console to get the keys I need? please give me some advice
config, err := common.ConfigurationProviderFromFile("./config", "")
if err != nil {
t.Error(err.Error())
}
c, err := objectstorage.NewObjectStorageClientWithConfigurationProvider(config)
if err != nil {
t.Error(err.Error())
}
https://cloud.oracle.com/identity/domains/my-profile/api-keys
I generated a key on this page, put it in my project, and with the above code I was able to get started without any problems.
Related
Hеllo, fellow Gophers!
Learning how to write unit tests and got stuck. Please, don't beat me up, cause I am new to this. I've googled and read a lot, but I still couldn't come up with a proper solution.
The exercise is to write a unit test for a function I wrote:
Filecopy(pathfrom, pathto string, limit, offset int64) error
The simplified function will look like this
source, err := os.Open(pathfrom)
destination, err := os.Create(pathto)
buf := make([]byte, *buffersize)
for {
n, err := source.Read(buf)
if err != nil && err != io.EOF {
return err
}
if _, err := destination.Write(buf[pos:n]); err != nil {
return err
if n == 0 {
break
}
}
}
Now I have to unit test it. And it's not going well. There are two ways I could test it:
Provide it a real temp file with a predetermined contents, so the test function would know what to expect after the execution. Should work, but I read this option is NOT the way to unit test properly as it touches the FS.
Use fake FS. I've tried testing files generated by mapfs and afero, but no luck, my function doesn't work with it (it works with real files though), either I am doing something wrong.
Please tell me how to unit test my function properly. Any help will be appreciated.
I'm creating a simple program to register my IP to OpenDNS when it changes. I know about the ddclient but I want to write my own just for learning purposes.
To be able to perform any operation on OpenDNS, I have to call the URL specifying my user and pass, so a curl example would be something like: curl -u user:password https://updates.opendns.com/nic/update?hostname=xxxx&myip=123.123.123.123
In Go I created the following function:
func registerNewIpToOpenDns(ip string) (int, error) {
openDnsURL := "https://updates.opendns.com/nic/update?hostname=xxxx&myip=" + ip
req, err := http.NewRequest("GET", openDnsURL, nil)
if err != nil {
return 0, err
}
req.SetBasicAuth("USER", "PASS")
resp, err := http.DefaultClient.Do(req)
if err != nil {
return 0, err
}
defer resp.Body.Close()
return resp.StatusCode, nil
}
So how should I perform the input of the user/pass to this program? I will let this project 'Public' in Github.
I was thinking in creating a file something like "input" and add it to .gitignore.
So if someone else wants to use the program, the person would just need to create it own "input" file and the program would read from it.
What do you think?
Put the configuration data that wouldn't be true for everybody in environment variables.
Use os.Getenv() to retrieve the variables at runtime. Make sure they are set to something valid looking (at least not empty string) as part of your program's configuration.
You can then set the environment variables in a systemd configuration file if you're running this from systemd, or in the .bash_config for a user dedicated to this process, or wherever is the most convenient for where your program is executed.
Or, create a configuration file you can read from your program. I usually use Json encoding for configuration like this, but you could use anything. Reading secrets from configuration files might arguably be somewhat safer than environment variables which can often be introspected by system processes.
When I create a configuration file, I usually model my configuration with a struct,
type Config struct {
Username string
Password string
}
Then as part of my program's initialization, I'd do something like
const ConfigFileEnv "ConfigFile" // avoid typing errors
var config Config
...
if f, err := os.Open(os.Getenv(ConfigFileEnv); err != nil {
panic(fmt.Errorf("Couldn't open config file %s: %w",
os.Getenv(ConfigFileEnv),
err,
))
} else if err := json.NewDecoder(f).Decode(&config); err != nil {
panic(fmt.Errorf("Couldn't decode json from config file %s: %w",
os.Getenv(ConfigFileEnv),
err
)
}
// Now config file has been loaded into config
...
req.SetBasicAuth(config.Username, config.Password)
Working minimal example (without your logic): https://github.com/farrellit/stackoverflow/tree/main/69335827
I try to run SQL queries from a Golang application using the official Tarantool client. The only way I know how to do it is by using conn.Eval like below. But I don't receive any errors. I can drop non existing tables, insert rows with duplicate keys. I will never find out that something went wrong.
resp, err := conn.Eval("box.execute([[TRUNCATE TABLE not_exists;]])", []interface{}{})
// err is always nil
// resp.Error is always empty
Can you point out the way to get errors or the right way to run SQL queries.
thanks for the question!
I have talked to the team and we have two options for you. Here is the first one:
resp, err := conn.Eval("return box.execute([[TRUNCATE TABLE \"not_exists\";]])", []interface{}{})
if len(resp.Tuples()) > 1 {
fmt.Println("Error", resp.Tuples()[1])
}else{
fmt.Println("Result", resp.Tuples()[0])
}
And here is the second one:
r, err := tnt.Eval("local data, err = box.execute(...) return data or box.error(err)", []interface{}{
`TRUNCATE table "not_exists";`,
})
if err != nil {
log.Fatalln(err)
}
I hope that helps! And if it doesn't - let me know and we will look into this one more time.
I'm trying to use the unstructured.UnstructuredList to reuse come logic for configmap and secret.
However, after adding the ListAndDeployReferredObject, I started to see tons of trace as Starting reflector *unstructured.Unstructured was added to my log file.
Am I doing something odd or I'm missing some setting for using the unstructured.Unstructured?
Thanks in advance.
func (r *ReconcileSubscription) ListAndDeployReferredObject(instance *appv1alpha1.Subscription, gvk schema.GroupVersionKind, refObj referredObject) error {
insName := instance.GetName()
insNs := instance.GetNamespace()
uObjList := &unstructured.UnstructuredList{}
uObjList.SetGroupVersionKind(gvk)
opts := &client.ListOptions{Namespace: insNs}
err := r.Client.List(context.TODO(), uObjList, opts)
if err != nil && !errors.IsNotFound(err) {
klog.Errorf("Failed to list referred objects with error %v ", err)
return err
}
// other logics...
}
I0326 23:05:58.955589 95169 reflector.go:120] Starting reflector *unstructured.Unstructured (10m0s) from pkg/mod/k8s.io/client-go#v0.0.0-20191016111102-bec269661e48/tools/cache/reflector.go:96
...
I0326 23:15:18.718932 95169 reflector.go:158] Listing and watching *unstructured.Unstructured from pkg/mod/k8s.io/client-go#v0.0.0-20191016111102-bec269661e48/tools/cache/reflector.go:96
I figured out these prints are normal, since we are using the dynamic client on our controller for caches
I'm trying to insert a map value into my Cassandra database. I'm using Go to write my client. Currently its throwing the error "can not marshal string into map(varchar, varchar)". I understand what the error is, but I can't resolve it. Here is the code that I've written.
if err := session.Query("INSERT INTO emergency_records
(mapColumn)
VALUES (?)",
"{'key' : 'value'}").Exec();
err != nil {
log.Fatal(err)
}
What I don't get is that I've written one query as a whole unbroken string and it works fine without throwing this error. Yet breaking it down with the question mark it throws the error. I know this is something simple that I'm just overlooking and couldn't find in the documentation, but any help would be great thanks.
I haven't used Go casandra client before but I guess passing map as a map instead of string should work:
mapValue := map[string]string{"key": "value"}
if err := session.Query("INSERT INTO emergency_records (mapColumn) VALUES (?)", mapValue).Exec(); err != nil {
log.Fatal(err)
}